validate_syntax( $php_code ); } // Fallback to original method $result = array( 'valid' => false, 'errors' => array(), 'message' => '', 'php_version' => '', 'execution_time' => 0 ); $start_time = microtime( true ); try { // Create temporary file $temp_file = $this->create_temp_file( $php_code ); if ( ! $temp_file ) { $result['errors'][] = 'Unable to create temporary file for validation'; $result['message'] = 'PHP syntax errors found'; return $result; } // Run PHP syntax check $output = array(); $return_code = 0; $command = $this->php_path . ' -l "' . $temp_file . '" 2>&1'; // Use exec-with-fallback if available if ( class_exists( 'ExecWithFallback\\ExecWithFallback' ) ) { $exec_result = \ExecWithFallback\ExecWithFallback::exec( $command, $output, $return_code ); } else { // Fallback to regular exec $exec_result = exec( $command, $output, $return_code ); } // Get PHP version $version_output = array(); if ( class_exists( 'ExecWithFallback\\ExecWithFallback' ) ) { \ExecWithFallback\ExecWithFallback::exec( $this->php_path . ' -v', $version_output ); } else { exec( $this->php_path . ' -v', $version_output ); } $result['php_version'] = ! empty( $version_output ) ? $version_output[0] : 'PHP ' . PHP_VERSION; // Parse results if ( $return_code === 0 ) { $result['valid'] = true; $result['message'] = 'PHP syntax is valid'; $result['errors'] = array(); } else { $result['valid'] = false; $result['message'] = 'PHP syntax errors found'; $result['errors'] = $this->parse_syntax_errors( $output ); } // Clean up $this->cleanup_temp_file( $temp_file ); } catch ( Exception $e ) { $result['errors'][] = 'Validation failed: ' . $e->getMessage(); $result['message'] = 'PHP syntax errors found'; } $result['execution_time'] = round( ( microtime( true ) - $start_time ) * 1000, 2 ); return $result; } /** * Create temporary file with PHP code * * @since 1.0.0 * @param string $php_code PHP code * @return string|false Temporary file path or false on failure */ private function create_temp_file( $php_code ) { // Ensure code has proper PHP tags if ( strpos( $php_code, ' 'parse_error', 'message' => trim( $matches[1] ), 'file' => basename( $matches[2] ), 'line' => intval( $matches[3] ), 'severity' => 'error' ); } // Parse fatal errors if ( preg_match( '/Fatal error:\s*(.+?)\s+in\s+(.+?)\s+on\s+line\s+(\d+)/i', $output_text, $matches ) ) { $errors[] = array( 'type' => 'fatal_error', 'message' => trim( $matches[1] ), 'file' => basename( $matches[2] ), 'line' => intval( $matches[3] ), 'severity' => 'error' ); } // Parse warnings if ( preg_match( '/Warning:\s*(.+?)\s+in\s+(.+?)\s+on\s+line\s+(\d+)/i', $output_text, $matches ) ) { $errors[] = array( 'type' => 'warning', 'message' => trim( $matches[1] ), 'file' => basename( $matches[2] ), 'line' => intval( $matches[3] ), 'severity' => 'warning' ); } // If no specific errors found, add generic error if ( empty( $errors ) && ! empty( $output_text ) ) { $errors[] = array( 'type' => 'unknown', 'message' => trim( $output_text ), 'file' => 'unknown', 'line' => 0, 'severity' => 'error' ); } return $errors; } /** * Get PHP configuration info * * @since 1.0.0 * @return array PHP configuration */ public function get_php_info() { $info = array( 'php_path' => $this->php_path, 'php_version' => PHP_VERSION, 'php_binary' => PHP_BINARY, 'exec_functions' => array(), 'disabled_functions' => array(), 'exec_with_fallback_available' => class_exists( 'ExecWithFallback\\ExecWithFallback' ) ); // Check available exec functions $exec_functions = array( 'exec', 'system', 'shell_exec', 'passthru', 'proc_open', 'popen' ); foreach ( $exec_functions as $func ) { $info['exec_functions'][ $func ] = function_exists( $func ) && ! in_array( $func, explode( ',', ini_get( 'disable_functions' ) ) ); } // Get disabled functions $disabled = ini_get( 'disable_functions' ); $info['disabled_functions'] = ! empty( $disabled ) ? explode( ',', $disabled ) : array(); return $info; } /** * Test if validation is available * * @since 1.0.0 * @return bool */ public function is_available() { try { $test_code = 'validate_syntax( $test_code ); return $result['success']; } catch ( Exception $e ) { return false; } } } endif;