Completed
Branch ENHANCE/255/add-wp-version-to-... (b44276)
by
unknown
24:50 queued 16:56
created

EE_Log::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * Class EE_Log
6
 *
7
 * Singleton logging class. Can be called from anywhere in the plugin to log data to a log file.
8
 * Defaults to wp-content/uploads/espresso/logs/espresso_log.txt
9
 * Usage:
10
 * do_action( 'AHEE_log', __FILE__, __FUNCTION__, 'logging message' );
11
 *
12
 * @package               Event Espresso
13
 * @subpackage            core
14
 * @author                Sidney Harrel, Brent Christensen
15
 *
16
 */
17
class EE_Log
18
{
19
20
    /**
21
     * @var string
22
     */
23
    private $_log = '';
24
25
    /**
26
     * Used for remote logging
27
     *
28
     * @var string
29
     */
30
    private $_remote_logging_url = '';
31
32
    /**
33
     * @var string
34
     */
35
    private $_remote_log = '';
36
37
    /**
38
     * @var EE_Log
39
     */
40
    private static $_instance;
41
42
43
    /**
44
     * @return EE_Log
45
     */
46
    public static function instance()
47
    {
48
        if (! self::$_instance instanceof EE_Log) {
49
            self::$_instance = new self();
50
        }
51
        return self::$_instance;
52
    }
53
54
    /**
55
     * @access private
56
     * @return EE_Log
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
57
     */
58
    private function __construct()
59
    {
60
61
        if (! EE_Registry::instance()->CFG->admin->use_remote_logging) {
62
            return;
63
        }
64
65
        $this->_remote_logging_url = EE_Registry::instance()->CFG->admin->remote_logging_url;
66
        $this->_remote_log = '';
67
68
        if (EE_Registry::instance()->CFG->admin->use_remote_logging) {
69
            add_action('shutdown', array($this, 'send_log'), 9999);
70
        }
71
    }
72
73
74
    /**
75
     *    verify_filesystem
76
     * tests that the required files and folders exist and are writable
77
     *
78
     */
79
    public function verify_filesystem()
80
    {
81
        $msg = esc_html__(
82
            'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.',
83
            'event_espresso'
84
        );
85
        EE_Error::doing_it_wrong(
86
            __METHOD__,
87
            $msg,
88
            '$VID:$'
89
        );
90
    }
91
92
93
    /**
94
     *    _format_message
95
     *    makes yer log entries look all purdy
96
     *
97
     * @param string $file
98
     * @param string $function
99
     * @param string $message
100
     * @param string $type
101
     * @return string
102
     */
103
    private function _format_message($file = '', $function = '', $message = '', $type = '')
104
    {
105
        $msg = '----------------------------------------------------------------------------------------' . PHP_EOL;
106
        $msg .= '[' . current_time('mysql') . '] ';
107
        $msg .= ! empty($file) ? basename($file) : '';
108
        $msg .= ! empty($file) && ! empty($function) ? ' -> ' : '';
109
        $msg .= ! empty($function) ? $function . '()' : '';
110
        $msg .= PHP_EOL;
111
        $type = ! empty($type) ? $type : 'log message';
112
        $msg .= ! empty($message) ? "\t" . '[' . $type . '] ' . $message . PHP_EOL : '';
113
        return $msg;
114
    }
115
116
117
    /**
118
     *    log
119
     * adds content to the EE_Log->_log property which gets written to file during the WP 'shutdown' hookpoint via the
120
     * EE_Log::write_log() callback
121
     *
122
     * @param string $file
123
     * @param string $function
124
     * @param string $message
125
     * @param string $type
126
     */
127
    public function log($file = '', $function = '', $message = '', $type = '')
128
    {
129
        $this->_log .= $this->_format_message($file, $function, $message, $type);
130
    }
131
132
133
    /**
134
     * write_log
135
     * appends the results of the 'AHEE_log' filter to the espresso log file
136
     */
137
    public function write_log()
138
    {
139
        $msg = esc_html__(
140
            'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.',
141
            'event_espresso'
142
        );
143
        EE_Error::doing_it_wrong(
144
            __METHOD__,
145
            $msg,
146
            '$VID:$'
147
        );
148
    }
149
150
151
    /**
152
     * send_log
153
     * sends the espresso log to a remote URL via a PHP cURL request
154
     */
155
    public function send_log()
156
    {
157
158
        if (empty($this->_remote_logging_url)) {
159
            return;
160
        }
161
162
        $data = 'domain=' . $_SERVER['HTTP_HOST'];
163
        $data .= '&ip=' . $_SERVER['SERVER_ADDR'];
164
        $data .= '&server_type=' . $_SERVER['SERVER_SOFTWARE'];
165
        $data .= '&time=' . time();
166
        $data .= '&remote_log=' . $this->_log;
167
        $data .= '&request_array=' . json_encode($_REQUEST);
168
        $data .= '&action=save';
169
170
        if (defined('EELOGGING_PASS')) {
171
            $data .= '&pass=' . EELOGGING_PASS;
172
        }
173
        if (defined('EELOGGING_KEY')) {
174
            $data .= '&key=' . EELOGGING_KEY;
175
        }
176
177
        $c = curl_init($this->_remote_logging_url);
178
        curl_setopt($c, CURLOPT_POST, true);
179
        curl_setopt($c, CURLOPT_POSTFIELDS, $data);
180
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
181
        curl_exec($c);
182
        curl_close($c);
183
    }
184
185
186
    /**
187
     * write_debug
188
     * writes the contents of the current request's $_GET and $_POST arrays to a log file.
189
     * previous entries are overwritten
190
     */
191
    public function write_debug()
192
    {
193
        $msg = esc_html__(
194
            'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.',
195
            'event_espresso'
196
        );
197
        EE_Error::doing_it_wrong(
198
            __METHOD__,
199
            $msg,
200
            '$VID:$'
201
        );
202
    }
203
204
205
    /**
206
     * __clone
207
     */
208
    public function __clone()
209
    {
210
        trigger_error(__('Clone is not allowed.', 'event_espresso'), E_USER_ERROR);
211
    }
212
}
213