DOMDocument->save() fails after user_abort is handled - php

In PHP, its possible to register shutdown functions, which (sometimes gets ignored, however) is definetely called in my scenario, see below.
PHP/libxml supported by the DOMDocument class in PHP does not play along well w/ my registered shutdown functions, if I want to call ->save() (->saveXML() works fine) after user abort (e.g. from registered shutdown function or a class instance destructor). Related is also the PHP connection handling.
Let the examples speak:
PHP version:
php --version
PHP 7.1.4 (cli) (built: Apr 25 2017 09:48:36) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
To reproduce user_abort I'm running the php through python2.7 run.py:
import subprocess
cmd = ["/usr/bin/php", "./user_aborted.php"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
# As this process exists here and the user_aborted.php
# has sleeps/blocks in for-cycle thus simulating a user-abort
# for the php subprocess.
The php script user_aborted.php to try saving XML in shutdown function :
<?php
ignore_user_abort(false);
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml_track = $xml->createElement( "Track", "Highway Blues" );
$xml->appendChild($xml_track);
function shutdown() {
global $xml;
$out_as_str = $xml->saveXML();
fwrite(STDERR, "\nout_as_str: " . var_export($out_as_str, true) . "\n");
$out_as_file = $xml->save('out.xml');
fwrite(STDERR, "\nout_as_file: >" . var_export($out_as_file, true) . "<\n");
fwrite(STDERR, "\nerrors: \n" . var_export(libxml_get_errors(), true) . "\n");
}
register_shutdown_function('shutdown');
$i = 2;
while ($i > 0) {
fwrite(STDERR, "\n PID: " . getmypid() . " aborted: " . connection_aborted());
echo("\nmaking some output on stdout"); // so user_abort will be checked
sleep(1);
$i--;
}
Now, if I run this script w/o user abort (simply calling PHP) with:
php user_aborted.php the XML gets saved properly.
However when calling this through python2.7 (which simulates the user abort by exiting the parent process), python2.7 run.py the weirdest things happen:
the out_as_str value is fine, looks the XML I wanted
BUT the file out.xml is an empty file
ALSO the libxml_get_errors reports FLUSH problems
The output w/ python looks:
python2.7 run.py
PID: 16863 aborted: 0
out_as_str: '<?xml version="1.0"?>
<Track>Highway Blues</Track>
'
out_as_file: >false<
errors:
array (
0 =>
LibXMLError::__set_state(array(
'level' => 2,
'code' => 1545,
'column' => 0,
'message' => 'flush error',
'file' => '',
'line' => 0,
))
)
Sorry for the long post, but I was looking through PHP/libxml2 code the whole day w/o any success. :/

Reason:
It turns out this is due to a fix for a previous bug.
Links:
previous PHP bug ticket which fix introduced the deffect
commit that introduces the deffect (GitHub)
The linked php_libxml_streams_IO_write function is the writecallback (set in ext/libxml/libxml.c) for the buffer of the docp object, which is handed over for the libxml call on ext/dom/document.c. Ending up in libxml xmlIO.c where the buffer is NULL hence the file given over for ->save(*) does not get written.
Workaround:
Use the ->saveXML() to get the XML representation in string and write it using file_put_contents(*) by "hand":
$xml_as_str = $xml->saveXML();
file_put_contents('/tmp/my.xml', $xml_as_str);

Related

PHP is my function called from echo, or sprintf

I want to look where the function is called.
Is the function called inside a echo or sprintf? Then return, otherwise echo the content.
I got this code (test.php):
<?php
function __($str = '')
{
// who is my parent?
$parent=debug_backtrace();
if ( isset ( $parent[1] ) )
$parent = $parent[1]['function']; //Did it!
else
$parent = debug_backtrace()[1]['function']; //Try
if ( empty ( $parent ) )
$parent = "ERROR!";
return sprintf("[%s] %s.%s", $parent, $str, PHP_EOL);
}
__('does nothing');
echo __('test from echo #1');
echo(__('test from echo #2'));
echo sprintf(__('test from sprintf #1'));
echo(sprintf(__('test from sprintf #2')));
?>
When I type it at the terminal all I get is:
WDGMBP:Test Wes$ php test.php
[ERROR!] test from echo #1.
[ERROR!] test from echo #2.
[ERROR!] test from sprintf #1.
[ERROR!] test from sprintf #2.
(p.s. from web the same)
My PHP version is:
WDGMBP:BIHappyV3 Wes$ php -v
PHP 5.5.27 (cli) (built: Aug 22 2015 18:20:44)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
You're confused about how debug_backtrace works. It returns the functions that were executing to make the call, not the function that will be called with the result. E.g. if you have:
function testit() {
someFunc(__('something'));
}
then $parent[1]['function'] will contain testit, not someFunc.
I don't think there's any way to get someFunc. That function isn't anywhere on the stack, because it doesn't get called until after __() returns.

How to Correctly Run Python Script from PHP [duplicate]

This question already has answers here:
Running a Python script from PHP
(10 answers)
Closed 7 years ago.
I have a python script that I would like to run from PHP. This is my PHP script:
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
And this is my Python script:
import sys, json
# Load the data that PHP sent us
try:
data = json.loads(sys.argv[1])
except:
print "ERROR"
sys.exit(1)
# Generate some data to send to PHP
result = {'status': 'Yes!'}
# Send it to stdout (to PHP)
print json.dumps(result)
I would like to be able to exchange data between PHP and Python, but the above error gives the output:
ERROR NULL
Where am I going wrong ?
:::::EDIT::::::
I ran this:
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$temp = json_encode($data);
$result= shell_exec('C:\Python27\python.exe test.py ' . "'" . $temp . "'");
echo $result;
I am getting No JSON object could be decoded
On my machine, the code works perfectly fine and displays:
array(1) {
'status' =>
string(4) "Yes!"
}
On the other hand, you may make a few changes to diagnose the issue on your machine.
Check the default version of Python. You can do this by running python from the terminal. If you see something like:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
you're fine. If you see that you are running Python 3, this could be an issue, since your Python script is written for Python 2. So:
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[...]
should be a clue.
Again from the terminal, run python myScript.py "[\"as\",\"df\",\"gh\"]". What do you see?
{"status": "Yes!"}
is cool. A different response indicates that the issue is probably with your Python script.
Check permissions. How do you run your PHP script? Do you have access to /path/to/? What about /path/to/myScript.php?
Replace your PHP code by:
<?php
echo file_get_contents("/path/to/myScript.php");
?>
Do you get the actual contents?
Now let's add a few debugging helpers in your PHP code. Since I imagine that you are not using a debugger, the simplest way is to print debug statements. This is OK for 10-LOC scripts, but if you need to deal with larger applications, invest your time in learning how to use PHP debuggers and how do use logging.
Here's the result:
/path/to/demo.php
<?php
$data = array('as', 'df', 'gh');
$pythonScript = "/path/to/myScript.py";
$cmd = array("python", $pythonScript, escapeshellarg(json_encode($data)));
$cmdText = implode(' ', $cmd);
echo "Running command: " . $cmdText . "\n";
$result = shell_exec($cmdText);
echo "Got the following result:\n";
echo $result;
$resultData = json_decode($result, true);
echo "The result was transformed into:\n";
var_dump($resultData);
?>
/path/to/myScript.py
import sys, json
try:
data = json.loads(sys.argv[1])
print json.dumps({'status': 'Yes!'})
except Exception as e:
print str(e)
Now run the script:
cd /path/to
php -f demo.php
This is what I get:
Running command: python /path/to/myScript.py '["as","df","gh"]'
Got the following result:
{"status": "Yes!"}
The result was transformed into:
array(1) {
'status' =>
string(4) "Yes!"
}
yours should be different and contain a hint about what is happening.
I got it to work by adding quotes around the argument!
Like so:
<?php
$data = array('as', 'df', 'gh');
$temp = json_encode($data);
echo shell_exec('python myScript.py ' . "'" . $temp . "'");
?>

PHP-Parsing Android sdk-repository XML

How can parse XML with this structure :
<sdk:sdk-repository xmlns:sdk="http://schemas.android.com/sdk/android/repository/7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
Generated on Thu Oct 22 10:16:34 PDT 2009 using eclair-sdk 17704: Platform. Addon. Tools. Doc.
-->
<sdk:platform>
<sdk:version>2.0</sdk:version>
<sdk:api-level>5</sdk:api-level>
<sdk:codename/>
<sdk:revision>01</sdk:revision>
<sdk:min-tools-rev>
<sdk:major>3</sdk:major>
</sdk:min-tools-rev>
<sdk:description>Android SDK Platform 2.0, revision 1</sdk:description>
<sdk:desc-url>http://developer.android.com/sdk/android-2.0.html</sdk:desc-url>
<sdk:obsolete/>
<sdk:archives>
<sdk:archive arch="any" os="linux">
<sdk:size>75095268</sdk:size>
<sdk:checksum type="sha1">be9be6a99ca32875c96ec7f91160ca9fce7e3c7d</sdk:checksum>
<sdk:url>android-2.0_r01-linux.zip</sdk:url>
</sdk:archive>
<sdk:archive arch="any" os="macosx">
<sdk:size>74956356</sdk:size>
<sdk:checksum type="sha1">2a866d0870dbba18e0503cd41e5fae988a21b314</sdk:checksum>
<sdk:url>android-2.0_r01-macosx.zip</sdk:url>
</sdk:archive>
<sdk:archive arch="any" os="windows">
<sdk:size>76288040</sdk:size>
<sdk:checksum type="sha1">aeb623217ff88b87216d6eb7dbc846ed53f68f57</sdk:checksum>
<sdk:url>android-2.0_r01-windows.zip</sdk:url>
</sdk:archive>
</sdk:archives>
<sdk:layoutlib>
<sdk:api>4</sdk:api>
</sdk:layoutlib>
<sdk:uses-license ref="android-sdk-license"/>
</sdk:platform>
...
how can get Version? and size?
and even get Generated date ?( on Thu Oct 22 10:16:34 PDT 2009 using eclair-sdk 17704: Platform. Addon. Tools. Doc. ) for each SDK Platform(sdk:platform)
very very TNX
Parsing the XML and parsing the comment in the file are two completely separate things.
Regarding the XML itself, you can use PHP's SimpleXml to parse it, but you'll have to remember to specify the namespace when accessing the children-elements.
We know we want namespaces, and we know which one we want, because each node that we're interested in is prefixed with something:. For example:
<sdk:version>2.0</sdk:version>
So, in this case, we are interested in the sdk namespace. You can find where the namespaces are defined in the top-most declaration node:
<sdk:sdk-repository xmlns:sdk="http://schemas.android.com/sdk/android/repository/7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This declares the sdk namespace to be http://schemas.android.com/sdk/android/repository/7. The 7 is a version number, so keep this in mind if you ever need to update to use different SDKs.
A small example of parsing the XML, using your sample XML, would be:
// load the file and create the XML element
$feed = file_get_contents("repository.xml");
$xml = new SimpleXmlElement($feed);
// define the namespace you need to use (we want xmlns:sdk)
$sdk = $xml->children("http://schemas.android.com/sdk/android/repository/7");
// get the "platform" root element and start using it's nodes
$platform = $sdk->platform;
echo 'Version: ' . $platform->version . "<br />";
echo 'Description: ' . $platform->description . "<br />";
foreach ($platform->archives->archive as $archive) {
echo 'Archive: ' . $archive->url . ", size: " . $archive->size . "<br />";
}
To match the Generated on ... line, you'll either need to use substring parsing or build a regex that can parse it. I'm opting for the latter option to save lines-of-code, but it's really up to you how you'd prefer to go about it.
If you wanted to use a regex, and this is very-specific to the output in your sample XML so you may need to adjust it to be more dynamic, you can use PHP's preg_match and a very verbose pattern:
$pattern = '/<!--[\r\n\s]*Generated on (.*) using eclair-sdk 17704: Platform\. Addon\. Tools\. Doc\.[\r\n\s]*-->/';
$comments = array();
preg_match($pattern, $feed, $comments);
echo $comments[1];
Output:
Thu Oct 22 10:16:34 PDT 2009

standard output impact SIGKILL?

I have a script to limit the execution time length of commands.
limit.php
<?php
declare(ticks = 1);
if ($argc<2) die("Wrong parameter\n");
$cmd = $argv[1];
$tl = isset($argv[2]) ? intval($argv[2]) : 3;
$pid = pcntl_fork();
if (-1 == $pid) {
die('FORK_FAILED');
} elseif ($pid == 0) {
exec($cmd);
posix_kill(posix_getppid(), SIGALRM);
} else {
pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
sleep($tl);
posix_kill($pid, SIGKILL);
die("TIMEOUT_KILLED : $pid");
}
Then I test this script with some commands.
TEST A
php limit.php "php -r 'while(1){sleep(1);echo PHP_OS;}'" 3
After 3s, we can find the processes were killed as we expected.
TEST B
Remove the output code and run again.
php limit.php "php -r 'while(1){sleep(1);}'" 3
Result looks not good, the process created by function "exec" was not killed like TEST A.
[alix#s4 tmp]$ ps aux | grep whil[e]
alix 4433 0.0 0.1 139644 6860 pts/0 S 10:32 0:00 php -r while(1){sleep(1);}
System info
[alix#s4 tmp]$ uname -a
Linux s4 2.6.18-308.1.1.el5 #1 SMP Wed Mar 7 04:16:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
[alix#s4 tmp]$ php -v
PHP 5.3.9 (cli) (built: Feb 15 2012 11:54:46)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
Why the processes killed in TEST A but not in TEST B? Does the output impact the SIGKILL?
Any suggestion?
There is a PIPE between php -r 'while(1){sleep(1);echo PHP_OS;} (process C) and it's parent (process B), posix_kill($pid, SIGKILL) sends KILL signal to process B, then process B is terminated, but process C doesn't know anything about the signal and continues to run and outputs something to the broken pipe, when process C receives the SIGPIPE signal but has no idea how to handle it so it exits.
You can verify it with strace (run php limit.php "strace php -r 'while(1){sleep(1); echo PHP_OS;};'" 1), and you will see something like this:
14:43:49.254809 write(1, "Linux", 5) = -1 EPIPE (Broken pipe)
14:43:49.254952 --- SIGPIPE (Broken pipe) # 0 (0) ---
14:43:49.255110 close(2) = 0
14:43:49.255212 close(1) = 0
14:43:49.255307 close(0) = 0
14:43:49.255402 munmap(0x7fb0762f2000, 4096) = 0
14:43:49.257781 munmap(0x7fb076342000, 1052672) = 0
14:43:49.258100 munmap(0x7fb076443000, 266240) = 0
14:43:49.258268 munmap(0x7fb0762f3000, 323584) = 0
14:43:49.258555 exit_group(0) = ?
As to php -r 'while(1){sleep(1);}, because there is no broken pipe occurs after it's parent dies, so it continues to run as expected.
Generally speaking, you should kill the whole process group but not only the process itself if you want to kill it's children too, with PHP you can add process B to its own process group, and kill the whole group then, here is the diff with your code:
--- limit.php 2012-08-11 20:50:22.000000000 +0800
+++ limit-new.php 2012-08-11 20:50:39.000000000 +0800
## -9,11 +9,13 ##
if (-1 == $pid) {
die('FORK_FAILED');
} elseif ($pid == 0) {
+ $_pid = posix_getpid();
+ posix_setpgid($_pid, $_pid);
exec($cmd);
posix_kill(posix_getppid(), SIGALRM);
} else {
pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
sleep($tl);
- posix_kill($pid, SIGKILL);
+ posix_kill(-$pid, SIGKILL);
die("TIMEOUT_KILLED : $pid");
}
You send the kill signall to your forked process, but that does not propagate to it's children or grandchildren. As such they are orphaned and continue running until something stops them from doing so. (In this case, any attempts to write to stdout should cause an error that then forces them to exit. Redirection of output would also probably result in indefinitely-running orphans.)
You want to send a kill signal to the process and all it's children. Unfortunately I lack the knowledge to tell you a good way to do that. I'm not very familiar with the process control functionality of PHP. Could parse the output of ps.
One simple way I found that works though is to send a kill signal to the whole process group with the kill command. It's messy, and it adds an extra "Killed" message to output on my machine, but it seems to work.
<?php
declare(ticks = 1);
if ($argc<2) die("Wrong parameter\n");
$cmd = $argv[1];
$tl = isset($argv[2]) ? intval($argv[2]) : 3;
$pid = pcntl_fork();
if (-1 == $pid) {
die('FORK_FAILED');
} elseif ($pid == 0) {
exec($cmd);
posix_kill(posix_getppid(), SIGALRM);
} else {
pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
sleep($tl);
$gpid = posix_getpgid($pid);
echo("TIMEOUT_KILLED : $pid");
exec("kill -KILL -{$gpid}"); //This will also cause the script to kill itself.
}
For more information see: Best way to kill all child processes

XMLRPC showing -32601 error (using PHP)

I have the following code...
<?php
include("lib/xmlrpc.inc");
$email='whatever#hotmail.com';
$c=new xmlrpc_client("/register/index.php", "ws.myserver.com", 80);
$f=new xmlrpcmsg('existsEmail', array(new xmlrpcval($email, "base64")));
print "<pre>" . htmlentities($f->serialize( )) . "</pre>";
$r=$c->send($f);
$v=$r->value( );
if (!$r->faultCode( )) {
print "Email is". $email . " is " .
$v->scalarval( ) . "<br />";
print "<hr />I got this value back<br /><pre>" .
htmlentities($r->serialize( )). "</pre><hr />\n";
} else {
print "Fault: ";
print "Code: " . $r->faultCode( ) .
" Reason '" .$r->faultString( )."'<br />";
}
?>
I need to consume the WebService located at http://ws.myserver.com/register/index.php.
I pass the email as a parameter and then the XMLRPC.inc library will encode it using base64.
I've got a good XML request shown below:
<?xml version="1.0"?>
<methodCall>
<methodName>existsEmail</methodName>
<params>
<param>
<value><base64>dnJvZHJpZ3VlekBpY2NrLm5ldC5jbw==</base64></value>
</param>
</params>
</methodCall>
BUUUT, when I tried to get a response from the server I've to the following error
Fault: Code: -32601 Reason 'server error. requested method not found'
Any ideas? I'm getting crazy about how to call the existsEmail method from my PHP code...I'm sure it is there but I don't know if I'm missing something..
You are getting an error message (Specification for Fault Code Interoperability, version 20010516) from the XMLRPC endpoint you're communicating with.
It is a defined error code:
-32601 ---> server error. requested method not found
The RPC method you requested was not found by the server. Contact the support of the service you consume to get a list of all available methods. If that method should be available, contact the support and discuss the issue with them.
You asked in comment:
Is there any way [to] verify which methods are available?
That depends on the service. XMLRPC on sourceforge has a suggestion of defined methods you can call to list information about the functions available:
XML-RPC Introspection
system.listMethods
system.methodSignature
system.methodHelp
You can try if it works with your service, too. AFAIK those are common, I wrapped up a quick example, you find the full code below. See the output below the code as well.
$path = 'http://xmlrpc-c.sourceforge.net/api/sample.php';
printf("\n XMLRPC Service Discovery\n\n for: '%s'\n\n", $path);
$discovery = new Discovery($path);
$methods = $discovery->getMethods();
printf(" Method Summary:\n ===============\n", count($methods));
foreach ($methods as $i => $method)
{
printf(" %'.-2d %s\n", $i + 1, $method->getName());
}
printf("\n Method Details (%d):\n ===================\n", count($methods));
foreach ($methods as $i => $method)
{
printf(" %'.-2d %s\n", $i + 1, $method->getName());
printf("\n %s\n", $method);
printf("\n%s\n\n", preg_replace('/^/um', ' ', wordwrap($method->getHelp(), 46)));
}
Output:
XMLRPC Service Discovery
for: 'http://xmlrpc-c.sourceforge.net/api/sample.php'
Method Summary:
===============
1. debug.authInfo
2. sample.add
3. sample.sumAndDifference
4. system.listMethods
5. system.methodHelp
6. system.methodSignature
Method Details (6):
===================
1. debug.authInfo
<struct> debug.authInfo
Report any HTTP authentication in use
2. sample.add
<int> sample.add (<int>, <int>)
Add two numbers
3. sample.sumAndDifference
<struct> sample.sumAndDifference (<int>, <int>)
Add and subtract two numbers
4. system.listMethods
<array> system.listMethods (<string>)
This method lists all the methods that the
XML-RPC server knows how to dispatch
5. system.methodHelp
<string> system.methodHelp (<string>)
Returns help text if defined for the method
passed, otherwise returns an empty string
6. system.methodSignature
<array> system.methodSignature (<string>)
Returns an array of known signatures (an array
of arrays) for the method name passed. If no
signatures are known, returns a none-array
(test for type != array to detect missing
signature)
You can find the sourcecode here: XMLRPC Discovery Service

Categories