Query IIS FastCGI settings from PHP - php

I am trying to programmatically query the FastCGI settings configured in IIS through PHP's COM API using WMI.
Using WMI CIM Studio I can see there is a FastCgiSection class which has a FastCgi member array that contains exactly the settings I want (specifically ActivityTimeout and RequestTimeout): http://msdn.microsoft.com/en-us/library/bb422421(v=vs.90).aspx
However, any attempt at querying this so far has not succeeded. The examples of querying Win32_Processor and so on that you can find online work fine, but translating that into a query of the FastCgiSection isn't working out.
So far I have this, which isn't outputting anything:
$wmi = new \COM('winmgmts:{impersonationLevel=Impersonate}//./root/WebAdministration');
$arrData = $wmi->ExecQuery("SELECT * FROM FastCgiSection");
foreach ($arrData as $obj) {
echo "has result";
}
How do I access this API through WMI in PHP?

Just in case you haven't already seen it, here is a pretty good link going into a lot of detail about doing very close to the same thing you're trying: http://www.sitepoint.com/php-wmi-dig-deep-windows-php/
One thing that I'd say to do ( if you haven't already ) is to check and verify the following is enabled in your php.ini: extension=php_com_dotnet.dll

Your query returns FastCgiSection objects, while the FastCGI application settings are stored in FastCgiApplicationElement class.
Your code doesn't access FastCgi member, only the objects returned by the WMI query. You need another loop over the FastCgi property in order to get what you want:
$wmi=new COM('winmgmts:{impersonationLevel=Impersonate}//./root/WebAdministration');
foreach($wmi->ExecQuery('SELECT * FROM FastCgiSection') as $section) {
foreach($section->FastCgi as $application) {
echo $application->ActivityTimeout, PHP_EOL;
echo $application->RequestTimeout, PHP_EOL;
}
}
Note that, in order for this code to work, you'll need to:
enable COM/.NET extension in "php.ini" file (as stated by Jonathon Hibbard).
install IIS 7's WMI Provider.

Related

Cannot get next results set when calling db2 stored procedure using php from Debian container to IBMi server

I am trying to call a db2 stored procedure using php (ibm_db2 v2.0.2 extension) from my Debian container to IBMi server.
I have installed ibm_db2 (v2.0.2) php extension on Debian container (docker) to request against a db2 database which is hosted on an IBMi server through TCP/IP protocol and it works just fine and I can make queries correctly.
My issue arise when I try to call a stored procedure with multiple results sets.
When I call a stored procedure, I am able to get the first results set using db2_fetch_assoc function but I cannot get the next results sets with db2_next_result function. There is no error thrown, the db2_fetch_assoc simply return false for the next results sets as if there was no subsequent sets.
The app calling theses stored procedure is deployed on an ZendServer on IBMi and everything works perfectly. The problem arise on my "docker" environment.
I tried many things like changing the version of ibm_db2 extension, trying another ODBC driver version, changing php configuration but I can't get it working.
The odd things is that when I run the stored procedure with db2 cli from my docker, I am able to get all my results sets correctly. It just doesn't work with php.
Coming to have some help. Did someone run into the same issue?
Thanks a lot,
EDIT 03/18/2020
Drivers info from db2_client_info php function
This is my code to retrieve my results sets:
while ($row = db2_fetch_assoc($statement)) {
print_r($row);
}
while ($row = db2_fetch_assoc(db2_next_result($statement))) {
print_r($row);
}
while ($row = db2_fetch_assoc(db2_next_result($statement))) {
print_r($row);
}
EDIT 03/18/2020 10:50
The php version is 7.1
The code now look like follow:
try {
db2_execute($statement);
while ($row = db2_fetch_assoc($statement)) {
print_r($row);
}
$nextResults = db2_next_result($statement);
while ($row = db2_fetch_assoc($nextResults)) {
print_r($row);
}
$nextResults = db2_next_result($statement);
while ($row = db2_fetch_assoc($nextResults)) {
print_r($row);
}
} catch (\Exception $e) {
echo $e->getMessage();
}
As per comment thread, the symptom was caused by incorrect coding.
For the db2_fetch_* calls to work, the preceding db2_next_result must return a result-set and you have to test for that.
In your original code you were unconditionally calling the db2_fetch_assoc with an invalid argument, as the db2_next_result did not return a result-set.
As per the code in both the IBM example, and in the PHP documentation, it is necessary to test the result of db2_next_result before calling any fetch action on.
The CLI trace showed a function sequence error, which tells you that the function is being called in the wrong circumstances.

Get the web server version in PHP

I would like the version of a web server (Nginx, MySQL, MariaDB, ...) in PHP.
I know the function for Apache: apache_get_version().
There are many phpinfo() which returns all values but how to exploit?
You would have an idea or it is not possible for the moment?
A simple shell_exec would do the trick (assuming you're on a unix based server). Just don't put any user data into the command, and be aware that this approach may not work in shared hosting environments:
$nginxVersion = shell_exec('nginx -v 2>&1');
$mysqlVersion = shell_exec('mysql --version');
Note that nginx sends version output to stderr, so you need to pipe it to stdout to capture it.
You can retrieve the web server's version by using the $_SERVER superglobal, more specifically by using:
$_SERVER['SERVER_SIGNATURE']
As per PHPs Documentation:
SERVER_SIGNATURE
String containing the server version and virtual host name which are added to server-generated pages, if enabled.
You can find more info on the official PHP Documentation site:
http://php.net/manual/en/reserved.variables.server.php
To query the version of MySQL and/or MariaDB in PHP, you could use mysqli_get_server_info() or (if you are on still using the deprecated older mysql API) mysql_get_server_info(). The PDO API has no similar function or class for that purpose, but in that case you could just use the result of the SQL query
SELECT VERSION();
It returns something like 5.5.50-0+deb7u2. Here's a quick example:
<?php
$user = 'username_here';
$pass = 'your_db_password';
// create DB connection
$dbh = new PDO('mysql:host=localhost;dbname=mysql', $user, $pass);
$stmt = $dbh->query('SELECT VERSION();');
//fetch first column of first result row and print it out
echo $stmt->fetchColumn();
//unset PDOStatement and PDO to close DB connection
unset($stmt);
unset($dbh);
?>
Address the $_SERVER super-global http://php.net/manual/en/reserved.variables.server.php
I believe the setting you want is:
echo $_SERVER['SERVER_NAME'];
The other answers mention different methods on how to get the web server and database versions, however there is a function which will get the operating system itself.
// Gets the "System" row shown at the top of the `phpinfo()` table.
$system = php_uname( $mode = 'a' );
You can specify the mode to select different components. By default it'll display all of the components, separated by spaces and in the following order:
s: Operating System Name
n: Host name
r: Release Name
v: Version Information
m: Machine Type
You can view more information about the php_uname function here.

Asynchronous snmp get calls in PHP

is there a good way to make some PHP calls asynchronous, non-blocking?
For example, take a look at this simple code:
<?php
$hosts = [...] // array of 100+ hosts
foreach ($hosts as $host){
$sysNames['$host'] = snmpget($host, 'community', "system.sysName.0");
}
echo 'done'
If, for example, 10 hosts are down, that will make a huge delay.
How to make snmpget calls non-blocking?
I've tried with React\Promise but I couldn't found some useful examples to start from. Can anyone suggest proper implementation of that class?
PHP supports multi-threading using the threads extensions; but it needs a properly build php-binary, plus some additional dll's if you're on Windows

Query logging for mySQL on shared server

I have a VPS with Dreamhost but the mySQL server is shared. I really want to start producing accessible logs of every mySQL query a particular site issues.
I can hand roll this into my abstraction layer but I was curious is there was something like sql_log_off that can be set at runtime so all queries get logged into files I can rotate and review?
From what I understand of what the question is asking:
What you could do is wrap your queries into some sort of wrapper that logs the queries into a file. This could be a text file or a PHP file that will only allow those with permission to view (a log viewer script could include this so that only those with proper access can view).
That is of course saying if you are able to do so. (If you are wanting to log queries from sites that you have no control over then I am not sure.)
An example of a wrapper you might be interested in:
function sql_query($query, $show=0)
{
global $queries, $debugginglist;
$thequery = mysql_query($query) or print(mysql_error()."<br>Query was: <code>".htmlspecialchars($query)."</code>");
$queries++;
if ($show == 1)
{
print "($queries): Query was: <i><code>".htmlspecialchars($query)."</code></i><br>";
}
$debugginglist .= "$qbr($queries): Query was: <i><code>$query</code></i><br>";
//this is just to give an idea for logging, NOT an exact solution
$logquery = fopen("querylog.txt", "ab+");
fputs($logquery, "\r\n$query");
fclose($logquery);
return $thequery;
}

How to do command line from PHP script

I need to write a script that will give users info on a given Unix account (the same Unix server that the script lives on). Mostly thing kinds of things that are in the passwd file or available via finger.
PHP is in safe-mode, so I can't access the passwd file via something built into php like file_get_contents(). Also, because it's in safe mode, various other command-line functions are disabled.
I thought I could get the info via a socket (no clue yet what that means, but I thought I'd try) but I get a fatal error that socket_create() is an unknown function. I pulled up the php-config file (which I can't change, FYI), and sure enough, sockets are not enabled.
However, while I was in there, I saw the line '--with-exec-dir=' with no actual directory set.
So then I remembered that when I was trying EVERY command line function, that some threw "not allowed in safe-mode" type errors, while others did nothing at all. If I put something like:
echo "[[";
exec("finger user");
echo "]]";
I'd end up with [[]]. So no errors, just no results either.
Bottom line:
Is there something I haven't tried? (in general)
Is there a runtime config option I can set to make exec() work?
quick note: I tried passthru() as well, specifically passthru("pwd") with still no output.
update
based on feedback, I tried both of the following:
$stuff = exec("pwd", $return);
echo "stuff=".$stuff."\n";
echo "return=";
print_r($return);
which results in:
stuff=
return=Array
(
)
and
$stuff = passthru("pwd", $return);
echo "stuff=".$stuff."\n";
echo "return=";
print_r($return);
which results in:
stuff=
return=1
The 1 sounds hopeful, but not what I want yet.
Idea
So this is actually an update of an already existing script that (please don't ask) I don't have access to. It's a perl script that's called via cgi. Is there a way to do php via cgi (so I don't have to deal with perl or rely on the older code)?
I'm afraid you can't do that in safe-mode. You have to remove the safe-mode if you have control of the server configuration.
I think you can't rely on sockets to read local files, sockets are used for network related things.
exec doesn't inherently return any data.
Try something like,
exec("finger user",$output);
echo "[[";
foreach($output as $key => $value){
echo $value;
}
echo "]]";
Exec returns a value, so do:
$var = exec("finger user");
and then parse the output to get what you want. You can get return status by adding in an optional variable thus:
exec("finger user", $var, $return_status);
or just:
echo exec("finger user");
if all you want is to see the output.
Thanks to all that responded, the following is what finally worked:
Create a cgi-bin folder
Add the following to the top of the php script:
#!/usr/local/bin/php-cgi
I don't know if this is something special on my server configuration, but I can run exec() and get what I'm after.

Categories