Completed
Branch dev (869f5d)
by
unknown
04:33
created

AdminPageFramework_Utility_SystemInformation   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getPHPInfo() 0 61 7
A getDefinedConstants() 0 17 4
A getPHPErrorLogPath() 0 8 2
A getPHPErrorLog() 0 10 2
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods to return various system information.
12
 *
13
 * @since       3.4.6
14
 * @extends     AdminPageFramework_Utility_File
15
 * @package     AdminPageFramework
16
 * @subpackage  Utility
17
 * @internal
18
 */
19
abstract class AdminPageFramework_Utility_SystemInformation extends AdminPageFramework_Utility_File {
20
    
21
    /**
22
     * Caches the result of PHP information array.
23
     * @since       3.4.6
24
     */
25
    static private $_aPHPInfo;
26
    
27
    /**
28
     * Returns the PHP information as an array.
29
     * 
30
     * @since       3.4.6
31
     */
32
    static public function getPHPInfo() {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
33
34
        if ( isset( self::$_aPHPInfo ) ) {
35
            return self::$_aPHPInfo;
36
        }
37
    
38
        ob_start();
39
        phpinfo( -1 );
40
41
        $_sOutput = preg_replace(
42
            array(
43
                '#^.*<body>(.*)</body>.*$#ms', '#<h2>PHP License</h2>.*$#ms',
44
                '#<h1>Configuration</h1>#',  "#\r?\n#", "#</(h1|h2|h3|tr)>#", '# +<#',
45
                "#[ \t]+#", '#&nbsp;#', '#  +#', '# class=".*?"#', '%&#039;%',
46
                '#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>'
47
                    .'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',
48
                '#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',
49
                '#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',
50
                "# +#",
51
                '#<tr>#',
52
                '#</tr>#'
53
            ),
54
            array(
55
                '$1', '', '', '', '</$1>' . "\n", '<', ' ', ' ', ' ', '', ' ',
56
                '<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'.
57
                "\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',
58
                '<tr><td>PHP Credits Egg</td><td>$1</td></tr>',
59
                '<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" . '<tr><td>Zend Egg</td><td>$1</td></tr>',
60
                ' ',
61
                '%S%',
62
                '%E%'
63
            ),
64
            ob_get_clean()
65
        );
66
67
        $_aSections = explode( '<h2>', strip_tags( $_sOutput, '<h2><th><td>' ) );
68
        unset( $_aSections[ 0 ] );
69
70
        $_aOutput = array();
71
        foreach( $_aSections as $_sSection ) {
72
            $_iIndex = substr( $_sSection, 0, strpos( $_sSection, '</h2>' ) );
73
            preg_match_all(
74
                '#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',
75
                $_sSection, 
76
                $_aAskApache, 
77
                PREG_SET_ORDER
78
            );
79
            foreach( $_aAskApache as $_aMatches ) {
80
                if ( ! isset( $_aMatches[ 1 ], $_aMatches[ 2 ] ) ) {
81
                    array_slice( $_aMatches, 2 );
82
                    continue;
83
                }
84
                $_aOutput[ $_iIndex ][ $_aMatches[ 1 ] ] = ! isset( $_aMatches[ 3 ] ) || $_aMatches[ 2 ] == $_aMatches[ 3 ]
85
                    ? $_aMatches[ 2 ] 
86
                    : array_slice( $_aMatches, 2 );
87
            }
88
        }
89
        self::$_aPHPInfo = $_aOutput;
90
        return self::$_aPHPInfo;   
91
    
92
    }  
93
            
94
    /**
95
     * Returns an array of constants.
96
     * 
97
     * @since       3.4.6
98
     * @param       array|string      $asCategory      The category key names of the returning array.
0 ignored issues
show
Bug introduced by
There is no parameter named $asCategory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
     */
100
    static public function getDefinedConstants( $asCategories=null, $asRemovingCategories=null ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
101
        
102
        $_aCategories           = is_array( $asCategories ) ? $asCategories : array( $asCategories );
103
        $_aCategories           = array_filter( $_aCategories );
104
        $_aRemovingCategories   = is_array( $asRemovingCategories ) ? $asRemovingCategories : array( $asRemovingCategories );
105
        $_aRemovingCategories   = array_filter( $_aRemovingCategories );
106
        $_aConstants            = get_defined_constants( true );
107
        
108
        if ( empty( $_aCategories ) ) {
109
            return self::dropElementsByKey( $_aConstants, $_aRemovingCategories );
110
        }
111
        return self::dropElementsByKey( 
112
            array_intersect_key( $_aConstants, array_flip( $_aCategories ) ),
113
            $_aRemovingCategories
114
        );
115
                
116
    }        
117
        
118
    /**
119
     * Returns PHP error log path.
120
     * 
121
     * @since       3.4.6
122
     * @return      array|string        The error log path. It can be multiple. If so an array holding them will be returned.
123
     */
124
    static public function getPHPErrorLogPath() {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
125
                
126
        $_aPHPInfo = self::getPHPInfo();
127
        return isset( $_aPHPInfo['PHP Core']['error_log'] ) 
128
            ? $_aPHPInfo['PHP Core']['error_log']
129
            : '';
130
        
131
    }
132
    
133
    /**
134
     * Returns a PHP error log.
135
     * @since       3.4.6
136
     */
137
    static public function getPHPErrorLog( $iLines=1 ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
138
        
139
        $_sLog = self::getFileTailContents( self::getPHPErrorLogPath(), $iLines );
140
        
141
        // If empty, it could be the log file could not be located. In that case, return the last error.
142
        return $_sLog
143
            ? $_sLog
144
            : print_r( @error_get_last(), true );   
145
        
146
    }        
147
        
148
}
149