| 1 |
<?php |
| 2 |
|
| 3 |
if ( !class_exists( 'MeowCommon_Helpers' ) ) { |
| 4 |
|
| 5 |
class MeowCommon_Helpers { |
| 6 |
|
| 7 |
public static $version = MeowCommon_Admin::version; |
| 8 |
|
| 9 |
static function is_divi_builder() { |
| 10 |
return isset( $_GET['et_fb'] ) && $_GET['et_fb'] === '1'; |
| 11 |
} |
| 12 |
|
| 13 |
static function is_cornerstone_builder() { |
| 14 |
return isset( $_GET['cs-render'] ) && $_GET['cs-render'] === '1'; |
| 15 |
} |
| 16 |
|
| 17 |
static function is_pagebuilder_request() { |
| 18 |
return self::is_divi_builder() || self::is_cornerstone_builder(); |
| 19 |
} |
| 20 |
|
| 21 |
static function is_asynchronous_request() { |
| 22 |
return self::is_ajax_request() || self::is_woocommerce_ajax_request() || self::is_rest(); |
| 23 |
} |
| 24 |
|
| 25 |
static function is_ajax_request() { |
| 26 |
return wp_doing_ajax(); |
| 27 |
} |
| 28 |
|
| 29 |
static function is_woocommerce_ajax_request() { |
| 30 |
return !empty( $_GET['wc-ajax'] ); |
| 31 |
} |
| 32 |
|
| 33 |
// Originally created by matzeeable, modified by jordymeow |
| 34 |
static function is_rest() { |
| 35 |
|
| 36 |
// WP_REST_Request init. |
| 37 |
$is_rest_request = defined('REST_REQUEST') && REST_REQUEST; |
| 38 |
if ( $is_rest_request ) { |
| 39 |
MeowCommon_Classes_Rest::init_once(); |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
// Plain permalinks. |
| 44 |
$prefix = rest_get_url_prefix(); |
| 45 |
$request_contains_rest = isset( $_GET['rest_route'] ) && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0; |
| 46 |
if ( $request_contains_rest) { |
| 47 |
MeowCommon_Classes_Rest::init_once(); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
// It can happen that WP_Rewrite is not yet initialized, so better to do it. |
| 52 |
global $wp_rewrite; |
| 53 |
if ( $wp_rewrite === null ) { |
| 54 |
$wp_rewrite = new WP_Rewrite(); |
| 55 |
} |
| 56 |
$rest_url = wp_parse_url( trailingslashit( rest_url() ) ); |
| 57 |
$current_url = wp_parse_url( add_query_arg( array() ) ); |
| 58 |
if ( !$rest_url || !$current_url ) |
| 59 |
return false; |
| 60 |
|
| 61 |
// URL Path begins with wp-json. |
| 62 |
if ( !empty( $current_url['path'] ) && !empty( $rest_url['path'] ) ) { |
| 63 |
$request_contains_rest = strpos( $current_url['path'], $rest_url['path'], 0 ) === 0; |
| 64 |
if ( $request_contains_rest) { |
| 65 |
MeowCommon_Classes_Rest::init_once(); |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
static function test_error( $error = 'timeout', $diceSides = 1 ) { |
| 74 |
if ( rand( 1, $diceSides ) === 1 ) { |
| 75 |
if ( $error === 'timeout' ) { |
| 76 |
header("HTTP/1.0 408 Request Timeout"); |
| 77 |
die(); |
| 78 |
} |
| 79 |
else { |
| 80 |
trigger_error( "Error", E_USER_ERROR); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static function php_error_logs() { |
| 86 |
$errorpath = ini_get( 'error_log' ); |
| 87 |
$output_lines = array(); |
| 88 |
if ( !empty( $errorpath ) && file_exists( $errorpath ) ) { |
| 89 |
try { |
| 90 |
$file = new SplFileObject( $errorpath, 'r' ); |
| 91 |
$file->seek( PHP_INT_MAX ); |
| 92 |
$last_line = $file->key(); |
| 93 |
$iterator = new LimitIterator( $file, $last_line > 3500 ? $last_line - 3500 : 0, $last_line ); |
| 94 |
$lines = iterator_to_array( $iterator ); |
| 95 |
$previous_line = null; |
| 96 |
foreach ( $lines as $line ) { |
| 97 |
|
| 98 |
// Parse the date |
| 99 |
$date = ''; |
| 100 |
try { |
| 101 |
$dateArr = []; |
| 102 |
preg_match( '~^\[(.*?)\]~', $line, $dateArr ); |
| 103 |
if ( isset( $dateArr[0] ) ) { |
| 104 |
$line = str_replace( $dateArr[0], '', $line ); |
| 105 |
$line = trim( $line ); |
| 106 |
$date = new DateTime( $dateArr[1] ); |
| 107 |
$date = get_date_from_gmt( $date->format('Y-m-d H:i:s'), 'Y-m-d H:i:s' ); |
| 108 |
} |
| 109 |
else { |
| 110 |
continue; |
| 111 |
} |
| 112 |
} |
| 113 |
catch ( Exception $e ) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
// Parse the error |
| 118 |
$type = ''; |
| 119 |
if ( preg_match( '/PHP Fatal error/', $line ) ) { |
| 120 |
$line = trim( str_replace( 'PHP Fatal error:', '', $line ) ); |
| 121 |
$type = 'fatal'; |
| 122 |
} |
| 123 |
else if ( preg_match( '/PHP Warning/', $line ) ) { |
| 124 |
$line = trim( str_replace( 'PHP Warning:', '', $line ) ); |
| 125 |
$type = 'warning'; |
| 126 |
} |
| 127 |
else if ( preg_match( '/PHP Notice/', $line ) ) { |
| 128 |
$line = trim( str_replace( 'PHP Notice:', '', $line ) ); |
| 129 |
$type = 'notice'; |
| 130 |
} |
| 131 |
else if ( preg_match( '/PHP Parse error/', $line ) ) { |
| 132 |
$line = trim( str_replace( 'PHP Parse error:', '', $line ) ); |
| 133 |
$type = 'parse'; |
| 134 |
} |
| 135 |
else if ( preg_match( '/PHP Exception/', $line ) ) { |
| 136 |
$line = trim( str_replace( 'PHP Exception:', '', $line ) ); |
| 137 |
$type = 'exception'; |
| 138 |
} |
| 139 |
else { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
// Skip the error if is the same as before. |
| 144 |
if ( $line !== $previous_line ) { |
| 145 |
array_push( $output_lines, array( 'date' => $date, 'type' => $type, 'content' => $line ) ); |
| 146 |
$previous_line = $line; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
catch ( OutOfBoundsException $e ) { |
| 151 |
error_log( $e->getMessage() ); |
| 152 |
return array(); |
| 153 |
} |
| 154 |
} |
| 155 |
return $output_lines; |
| 156 |
|
| 157 |
// else { |
| 158 |
// $output_lines = array_reverse( $output_lines ); |
| 159 |
// $html = ''; |
| 160 |
// $previous = ''; |
| 161 |
// foreach ( $output_lines as $line ) { |
| 162 |
// // Let's avoid similar errors, since it's not useful. We should also make this better |
| 163 |
// // and not only theck this depending on tie. |
| 164 |
// if ( preg_replace( '/\[.*\] PHP/', '', $previous ) !== preg_replace( '/\[.*\] PHP/', '', $line ) ) { |
| 165 |
// $html .= $line; |
| 166 |
// $previous = $line; |
| 167 |
// } |
| 168 |
// } |
| 169 |
// return $html; |
| 170 |
// } |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// Asked by WP Security Team to remove this. |
| 175 |
|
| 176 |
// if ( MeowCommon_Helpers::is_rest() ) { |
| 177 |
// ini_set( 'display_errors', 0 ); |
| 178 |
// } |
| 179 |
} |
| 180 |
|
| 181 |
?> |