Fatal error: Cannot redeclare class MinecraftQueryException - php

I have this error in my WordPress regarding this plugin, any ideas on how to fix it?
Thank you very much! ;D
<?php
class MinecraftQueryException extends Exception
{
// Exception thrown by MinecraftQuery class
}
class MinecraftQuery
{
/*
* Class written by xPaw
*
* Website: http://xpaw.me
* GitHub: https://github.com/xPaw/PHP-Minecraft-Query
*/
const STATISTIC = 0x00;
const HANDSHAKE = 0x09;
private $Socket;
private $Players;
private $Info;
public function Connect( $Ip, $Port = 25565, $Timeout = 3 )
{
if( !is_int( $Timeout ) || $Timeout < 0 )
{
throw new InvalidArgumentException( 'Timeout must be an integer.' );
}
$this->Socket = #FSockOpen( 'udp://' . $Ip, (int)$Port, $ErrNo, $ErrStr, $Timeout );
if( $ErrNo || $this->Socket === false )
{
throw new MinecraftQueryException( 'Could not create socket: ' . $ErrStr );
}
Stream_Set_Timeout( $this->Socket, $Timeout );
Stream_Set_Blocking( $this->Socket, true );
try
{
$Challenge = $this->GetChallenge( );
$this->GetStatus( $Challenge );
}
// We catch this because we want to close the socket, not very elegant
catch( MinecraftQueryException $e )
{
FClose( $this->Socket );
throw new MinecraftQueryException( $e->getMessage( ) );
}
FClose( $this->Socket );
}
public function GetInfo( )
{
return isset( $this->Info ) ? $this->Info : false;
}
public function GetPlayers( )
{
return isset( $this->Players ) ? $this->Players : false;
}
private function GetChallenge( )
{
$Data = $this->WriteData( self :: HANDSHAKE );
if( $Data === false )
{
throw new MinecraftQueryException( 'Failed to receive challenge.' );
}
return Pack( 'N', $Data );
}
private function GetStatus( $Challenge )
{
$Data = $this->WriteData( self :: STATISTIC, $Challenge . Pack( 'c*', 0x00, 0x00, 0x00, 0x00 ) );
if( !$Data )
{
throw new MinecraftQueryException( 'Failed to receive status.' );
}
$Last = '';
$Info = Array( );
$Data = SubStr( $Data, 11 ); // splitnum + 2 int
$Data = Explode( "\x00\x00\x01player_\x00\x00", $Data );
if( Count( $Data ) !== 2 )
{
throw new MinecraftQueryException( 'Failed to parse server\'s response.' );
}
$Players = SubStr( $Data[ 1 ], 0, -2 );
$Data = Explode( "\x00", $Data[ 0 ] );
// Array with known keys in order to validate the result
// It can happen that server sends custom strings containing bad things (who can know!)
$Keys = Array(
'hostname' => 'HostName',
'gametype' => 'GameType',
'version' => 'Version',
'plugins' => 'Plugins',
'map' => 'Map',
'numplayers' => 'Players',
'maxplayers' => 'MaxPlayers',
'hostport' => 'HostPort',
'hostip' => 'HostIp',
'game_id' => 'GameName'
);
foreach( $Data as $Key => $Value )
{
if( ~$Key & 1 )
{
if( !Array_Key_Exists( $Value, $Keys ) )
{
$Last = false;
continue;
}
$Last = $Keys[ $Value ];
$Info[ $Last ] = '';
}
else if( $Last != false )
{
$Info[ $Last ] = $Value;
}
}
// Ints
$Info[ 'Players' ] = IntVal( $Info[ 'Players' ] );
$Info[ 'MaxPlayers' ] = IntVal( $Info[ 'MaxPlayers' ] );
$Info[ 'HostPort' ] = IntVal( $Info[ 'HostPort' ] );
// Parse "plugins", if any
if( $Info[ 'Plugins' ] )
{
$Data = Explode( ": ", $Info[ 'Plugins' ], 2 );
$Info[ 'RawPlugins' ] = $Info[ 'Plugins' ];
$Info[ 'Software' ] = $Data[ 0 ];
if( Count( $Data ) == 2 )
{
$Info[ 'Plugins' ] = Explode( "; ", $Data[ 1 ] );
}
}
else
{
$Info[ 'Software' ] = 'Vanilla';
}
$this->Info = $Info;
if( $Players )
{
$this->Players = Explode( "\x00", $Players );
}
}
private function WriteData( $Command, $Append = "" )
{
$Command = Pack( 'c*', 0xFE, 0xFD, $Command, 0x01, 0x02, 0x03, 0x04 ) . $Append;
$Length = StrLen( $Command );
if( $Length !== FWrite( $this->Socket, $Command, $Length ) )
{
throw new MinecraftQueryException( "Failed to write on socket." );
}
$Data = FRead( $this->Socket, 4096 );
if( $Data === false )
{
throw new MinecraftQueryException( "Failed to read from socket." );
}
if( StrLen( $Data ) < 5 || $Data[ 0 ] != $Command[ 2 ] )
{
return false;
}
return SubStr( $Data, 5 );
}
}
The error message is this:
Cannot redeclare class MinecraftQueryException
plugins/server-status-for-minecraft-pc-pe/libs/MinecraftQuery.php on
line 5

Related

I have used return create_function in my application below. php 7.2

how to win?
PHP 7.2.0, the create_function() is deprecated.
Thanks for your help,
or wait for developers?
Edited and cleared Code
return create_function( '', "
global $chery_core_version;
$path = trailingslashit( dirname( __FILE__ ) ) . 'cherry-core.php';
$data = get_file_data( $path, array(
'version' => 'Version'
) );
if ( isset( $data['version'] ) ) {
$version = $data['version'];
}
$old_versions = null;
if ( null !== $chery_core_version ) {
$old_versions = array_keys( $chery_core_version );
}
if ( is_array( $old_versions ) && isset( $old_versions[0] ) ) {
$compare = version_compare( $old_versions[0], $version, '<' );
if ( $compare ) {
$chery_core_version = array();
$chery_core_version[ $version ] = $path;
}
} else {
$chery_core_version = array();
$chery_core_version[ $version ] = $path;
}
" );
Use anonymous functions instead:
return function () {
global $chery_core_version;
$path = trailingslashit(__DIR__) . 'cherry-core.php';
$data = get_file_data( $path, [
'version' => 'Version'
] );
if ( isset( $data['version'] ) ) {
$version = $data['version'];
}
$old_versions = null;
if ( null !== $chery_core_version ) {
$old_versions = array_keys( $chery_core_version );
}
if ( is_array( $old_versions ) && isset( $old_versions[0] ) ) {
$compare = version_compare( $old_versions[0], $version, '<' );
if ( $compare ) {
$chery_core_version = [];
$chery_core_version[ $version ] = $path;
}
} else {
$chery_core_version = [];
$chery_core_version[ $version ] = $path;
}
};

How add font php

I have such a small request because I have a php code and I do not know how to add a font to it, I know I have to add an imageloadfont but when it adds the generator just does not work. I've tried everything in my mind and nothing at all.
<?php
if( isSet( $_GET[ 'Address' ] ) )
{
$szAddress = $_GET[ 'Address' ];
$szExploded = explode( ":", $szAddress );
$szServerIP = $szExploded[ 0 ];
$szServerPort = $szExploded[ 1 ];
$Query = new SourceQuery( );
$Info = Array( );
$Rules = Array( );
$Players = Array( );
try
{
$Query -> Connect( $szServerIP, $szServerPort, DEF__TIME_OUT);
$Info = $Query -> GetInfo( );
$Players = $Query -> GetPlayers( );
$Rules = $Query -> GetRules( );
}
catch( Exception $e )
{
$Exception = $e;
}
$Query -> Disconnect( );
header( "Content-type: image/png" );
$rImage = imagecreatefrompng( DEF__IMAGE_NAME );
$iColor = imagecolorallocate( $rImage, DEF__COLOR_RED, DEF__COLOR_GREEN, DEF__COLOR_BLUE );
// bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
imagestring( $rImage, DEF__ADDRESS_TEXT_SIZE, DEF__ADDRESS_X, DEF__ADDRESS_Y, $szAddress, $iColor );
if( $Info[ 'MaxPlayers' ] != 0 )
{
$szHostName = MIRAGE;
$szMapName = $Info[ 'Map' ];
if( strlen( $szHostName ) > DEF__HOSTNAME_MAX_TEXT_LEN )
{
$szHostName = substr( $Info[ 'HostName' ], 0, DEF__HOSTNAME_MAX_TEXT_LEN ). ' ...';
}
if( strlen( $szMapName ) > DEF__MAP_MAX_TEXT_LEN )
{
$szMapName = substr( $szMapName, 0, DEF__MAP_MAX_TEXT_LEN ). '..';
}
imagestring( $rImage, DEF__HOSTNAME_TEXT_SIZE, DEF__HOSTNAME_X, DEF__HOSTNAME_Y, $szHostName, $iColor );
imagestring( $rImage, DEF__MAP_TEXT_SIZE, DEF__MAP_X, DEF__MAP_Y, $szMapName, $iColor );
imagestring( $rImage, DEF__PLAYERS_TEXT_SIZE, DEF__PLAYERS_X, DEF__PLAYERS_Y, $Info[ 'Players' ]. '/' .$Info[ 'MaxPlayers' ], $iColor );
}
else
{
imagestring( $rImage, DEF__PLAYERS_TEXT_SIZE, DEF__PLAYERS_X, DEF__PLAYERS_Y, "SERVER OFF", $iColor );
}
$font_width = ImageFontWidth(5);
imagepng( $rImage );
imagedestroy( $rImage );
}
?>

How to show gravity form uploaded file in post page

I am working with multi file upload with gravity form. So after uploading I want to show the uploaded file in single post page (single.php). How to show the uploaded files in the post page. I have been using this.
(http://pastebin.com/r9w2beDW)
class GW_Multi_File_Merge_Tag {
private static $instance = null;
private $_merge_tag_args = array();
private $_settings = array();
private function __construct() {
add_filter( 'gform_pre_replace_merge_tags', array( $this, 'replace_merge_tag' ), 10, 7 );
}
public static function get_instance() {
if( null == self::$instance )
self::$instance = new self;
return self::$instance;
}
public function get_default_args() {
return array(
'form_id' => false,
'exclude_forms' => array(),
'default_markup' => '{filename}.{ext}',
'markup' => array(
array(
'file_types' => array( 'jpg', 'png', 'gif' ),
'markup' => ''
),
array(
'file_types' => array( 'mp4', 'ogg', 'webm' ),
'markup' => '
Your browser does not support the video tag.
'
),
array(
'file_types' => array( 'ogv' ),
'markup' => '
Your browser does not support the video tag.
'
)
)
);
}
public function register_settings( $args = array() ) {
$args = wp_parse_args( $args, $this->get_default_args() );
if( ! $args['form_id'] ) {
$this->_settings['global'] = $args;
} else {
$this->_settings[$args['form_id']] = $args;
}
}
public function replace_merge_tag( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
foreach( $matches as $match ) {
$input_id = $match[1];
$field = GFFormsModel::get_field( $form, $input_id );
if( ! $this->is_applicable_field( $field ) )
continue;
if( $format != 'html' ) {
$value = $this->_merge_tag_args['value'];
} else {
if( $entry['id'] == null && is_callable( array( 'GWPreviewConfirmation', 'preview_image_value' ) ) ) {
$files = GWPreviewConfirmation::preview_image_value( 'input_' . $field->id, $field, $form, $entry );
} else {
$value = GFFormsModel::get_lead_field_value( $entry, $field );
$files = empty( $value ) ? array() : json_decode( $value, true );
}
$value = '';
foreach( $files as &$file ){
$value .= $this->get_file_markup( $file, $form['id'] );
}
}
$text = str_replace( $match[0], $value, $text );
}
return $text;
}
public function get_file_markup( $file, $form_id ) {
$value = str_replace( " ", "%20", $file );
$file_info = pathinfo( $value );
extract( $file_info ); // gives us $dirname, $basename, $extension, $filename
if( ! $extension )
return $value;
$markup_settings = $this->get_markup_settings( $form_id );
if( empty( $markup_settings ) )
return $value;
$markup_found = false;
foreach( $markup_settings as $file_type_markup ) {
$file_types = array_map( 'strtolower', $file_type_markup['file_types'] );
if( ! in_array( strtolower( $extension ), $file_types ) )
continue;
$markup_found = true;
$markup = $file_type_markup['markup'];
$tags = array(
'{url}' => $file,
'{filename}' => $filename,
'{basename}' => $basename,
'{ext}' => $extension
);
foreach( $tags as $tag => $tag_value ) {
$markup = str_replace( $tag, $tag_value, $markup );
}
$value = $markup;
break;
}
if( ! $markup_found && $default_markup = $this->get_default_markup( $form_id ) ) {
$tags = array(
'{url}' => $file,
'{filename}' => $filename,
'{basename}' => $basename,
'{ext}' => $extension
);
foreach( $tags as $tag => $tag_value ) {
$default_markup = str_replace( $tag, $tag_value, $default_markup );
}
$value = $default_markup;
}
return $value;
}
public function get_markup_settings( $form_id ) {
$form_markup_settings = rgars( $this->_settings, "$form_id/markup" ) ? rgars( $this->_settings, "$form_id/markup" ) : array();
$global_markup_settings = rgars( $this->_settings, 'global/markup' ) ? rgars( $this->_settings, 'global/markup' ) : array();
return array_merge( $form_markup_settings, $global_markup_settings );
}
public function get_default_markup( $form_id ) {
$default_markup = rgars( $this->_settings, "$form_id/default_markup" );
if( ! $default_markup )
$default_markup = rgars( $this->_settings, 'global/default_markup' );
return $default_markup;
}
public function is_excluded_form( $form_id ) {
$has_global_settings = isset( $this->_settings['global'] );
$excluded_forms = (array) rgars( $this->_settings, 'global/exclude_forms' );
$explicity_excluded = $has_global_settings && in_array( $form_id, $excluded_forms );
$passively_excluded = ! $has_global_settings && ! isset( $this->_settings[$form_id] );
return $explicity_excluded || $passively_excluded;
}
public function is_applicable_field( $field ) {
$is_valid_form = ! $this->is_excluded_form( $field['formId'] );
$is_file_upload_filed = GFFormsModel::get_input_type( $field ) == 'fileupload';
$is_multi = rgar( $field, 'multipleFiles' );
return $is_valid_form && $is_file_upload_filed && $is_multi;
}
}
function gw_multi_file_merge_tag() {
return GW_Multi_File_Merge_Tag::get_instance();
}
gw_multi_file_merge_tag()->register_settings( array(
'form_id' => 1,
'markup' => array(
array(
'file_types' => array( 'jpg', 'jpeg', 'png', 'gif' ),
'markup' => '{filename}'
),
array(
'file_types' => array( 'pdf', 'txt' ),
'markup' => '{filename}{filenam}'
),
array(
'file_types' => array( 'mp4', 'ogg', 'webm' ),
'markup' => 'Your browser does not support the video tag.{filename}'
),
array(
'file_types' => array( 'ogv' ),
'markup' => 'Your browser does not support the video tag.{filename}'
)
)
) );
But cannot show the file or file link in post page. Can anyone help me?

unable to call static helpers in php

I am using a class that I downloaded; being not too familiar with php classes and helpers, I decided to post it to see if anyone more experience can give some help with the error I am having.
I am calling the function in the script "torrenttest" using this
<?php
require_once 'Torrentmaker.php';
$torrent = new Torrent( 'file', 'http://fr33dom.h33t.com:3310/announce' );
$torrent->announce(array('udp://tracker.openbittorrent.com:80/announce',
if ( ! $error = $torrent->error() ) // error method return the last error message
$torrent->save('test2.torrent'); // save to disk
else
echo '<br>DEBUG: ',$error;
the problem is that when 'file' is on the same server it works fine; however when it is calling an distant server it gives an time-out error or just creates a torrent file with no size (fails to run)
this is the script of Torrentmaker.php
class Torrent {
public function __construct ( $data = null, $meta = array(), $piece_length = 256 ) {
if ( is_null( $data ) )
return false;
if ( $piece_length < 32 || $piece_length > 4096 )
return self::set_error( new Exception( 'Invalid piece lenth, must be between 32 and 4096' ) );
if ( is_string( $meta ) )
$meta = array( 'announce' => $meta );
if ( $this->build( $data, $piece_length * 1024 ) )
$this->touch();
else
$meta = array_merge( $meta, $this->decode( $data ) );
foreach( $meta as $key => $value )
$this->{$key} = $value;
public function content ( $precision = null ) {
$files = array();
if ( isset( $this->info['files'] ) && is_array( $this->info['files'] ) )
foreach ( $this->info['files'] as $file )
$files[self::path( $file['path'], $this->info['name'] )] = $precision ?
self::format( $file['length'], $precision ) :
$file['length'];
elseif ( isset( $this->info['name'] ) )
$files[$this->info['name']] = $precision ?
self::format( $this->info['length'], $precision ) :
$this->info['length'];
return $files;
}
public function save ( $filename = null ) {
return file_put_contents( is_null( $filename ) ? $this->info['name'] . '.torrent' : $filename, $this->encode( $this ) );
}
static public function file_get_contents ( $file, $timeout = self::timeout, $offset = null, $length = null ) {
if ( is_file( $file ) || ini_get( 'allow_url_fopen' ) ) {
$context = ! is_file( $file ) && $timeout ?
stream_context_create( array( 'http' => array( 'timeout' => $timeout ) ) ) :
null;
return ! is_null( $offset ) ? $length ?
#file_get_contents( $file, false, $context, $offset, $length ) :
#file_get_contents( $file, false, $context, $offset ) :
#file_get_contents( $file, false, $context );
} elseif ( ! function_exists( 'curl_init' ) )
return self::set_error( new Exception( 'Install CURL or enable "allow_url_fopen"' ) );
$handle = curl_init( $file );
if ( $timeout )
curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
if ( $offset || $length )
curl_setopt( $handle, CURLOPT_RANGE, $offset . '-' . ( $length ? $offset + $length -1 : null ) );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
$content = curl_exec( $handle );
$size = curl_getinfo( $handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD );
curl_close( $handle );
return ( $offset && $size == -1 ) || ( $length && $length != $size ) ? $length ?
substr( $content, $offset, $length) :
substr( $content, $offset) :
$content;
public function errors() {
return empty( self::$errors ) ?
false :
self::$errors;
}
}

How to make a time limit for sockets?

How to make socket time limit ?
http://pastebin.com/0q3NeLAX
I tried socket_time_limit and others, but didnt help.
I want that if socket does not received any information, it will be closed after X seconds.
function QueryMinecraft( $IP, $Port = 25565 )
{
$Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
if( $Socket === FALSE || #Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
{
return FALSE;
}
Socket_Send( $Socket, "\xFE", 1, 0 );
$Len = Socket_Recv( $Socket, $Data, 256, 0 );
Socket_Close( $Socket );
if( $Len < 4 || $Data[ 0 ] != "\xFF" )
{
return FALSE;
}
$Data = SubStr( $Data, 3 );
$Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
$Data = Explode( "\xA7", $Data );
return Array(
'HostName' => SubStr( $Data[ 0 ], 0, -1 ),
'Players' => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
);
}
I would not use the sockets extension for this, I only use that for advanced socket operations that cannot be done using fsockopen(). The reason for this is that the sockets extension is not always available, whereas fsockopen() usually is.
Here is how I would write your code, with a data receive timeout:
function QueryMinecraft( $IP, $Port = 25565 )
{
// Seconds to wait for a successful connection
$connectTimeout = 5;
// Seconds to wait for data
$receiveTimeout = 5;
$Socket = fsockopen($IP, $port, $errNo, $errStr, $connectTimeout);
if( !$Socket || !stream_set_timeout( $Socket, $receiveTimeout ) )
{
return FALSE;
}
fwrite( $Socket, "\xFE" );
$Data = fread( $Socket, 256 );
fclose( $Socket );
if( strlen( $Data ) < 4 || $Data[ 0 ] != "\xFF" )
{
return FALSE;
}
$Data = SubStr( $Data, 3 );
$Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
$Data = Explode( "\xA7", $Data );
return Array(
'HostName' => SubStr( $Data[ 0 ], 0, -1 ),
'Players' => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0
);
}

Categories