Fetch the list of shared drives using PHP on Windows? - php

I want to display the list of shared drives connected to my system running Windows. Is there any tutorial for this?
I have tried the following code in PHP:
echo "<select id = 'drives'><option>Drives</option>";
for ($ii=66;$ii<92;$ii++)
{
$char = chr($ii);
if (opendir($char.":/"))
echo "<option>".$char."</option>";
}
echo "</select>";

You have to use the experimental Win32 API functions in PHP:
Register the API function GetDriveType using w32api-register-function.
After that you can call this function either by using w32api-invoke-function or by directly calling GetDriveType. It depends on your PHP version. (See the user contributed note at the bottom of the documentation of w32api-invoke-function!)
Check the return value if it is DRIVE_REMOTE. You can find the required parameters and the list of other return values at the MSDN documentation of GetDriveType.

You can enumerate Windows shared resources via PHP's COM API:
$wmi = new COM('WinMgmts:{impersonationLevel=impersonate}!root/cimv2');
$result = $wmi->ExecQuery('Select Path from Win32_Share where Type = 0');
foreach ($result as $share) {
echo $share->Path, PHP_EOL;
}
The output will list all shared folders on the machine. Type 0 is a disk drive.
See the MSDN documentation for Win32_Share for additional properties you can query.
Note: if you are looking for Mapped Network Drives, change the query to
Select ProviderName From Win32_LogicalDisk Where DriveType = 4
and change $share->Path to $share->ProviderName
See this Technet Article for details.
An alternative to using COM would be to use the wmic command:
exec('wmic Share where Type=0 get Path', $shares);
print_r($shares);
is equivalent to the first example above. For more details on WMIC, see
WMIC - Take Command-line Control over WMI and
Windows Management Instrumentation Command

This may help to do it under c#. http://www.codeproject.com/KB/IP/networkshares.aspx
I can't find any way to do it directly under php.
To use this with your php you may need make this as a dll and call it from php using the Win32 api functions.

Related

Invoke Python API from PHP

how i can reproduce the same thing in PHP:
import xmlrpclib
s = xmlrpclib.Server("http://domainname.com/Verify.py", verbose = False)
print s.verify({"var1" : "test","var2" : "A" })
print s.verify({"var1" : "test","var2" : "H" })
XML-RPC is nice because it's language agnostic. As long as XML-RPC libraries are provided by a language, we can use that language. Assume A is your local machine and B is the server. In your example, both A (client) and B are running Python.
The official PHP api for XML-RPC can be found on the php site. However, i it's pretty bare since there's no documentation. I recommend grabbing the code from this repo: https://github.com/gggeek/phpxmlrpc/ which has much better documentation. So, to answer your question, we just need to two things:
Set up client to query the server
Call server to get result
Converting your code sample to PHP would then look something like:
$client = new xmlrpc_client("/Verify.py", "http://domainname.com", 80);
$message = new xmlrpcmsg("verify",
array(new xmlrpcval(test, "string")));
$response = $client->send($message);
$result = $response->value
Just for fun, let's reverse things... Now assume that you want to call a PHP function on the XML-RPC server using Python. In other words, A is running Python and B is running PHP.
import xmlrpclib
# Create an object to represent server.
server = xmlrpclib.Server("http://domainname.com/functions.php");
# Call the server to get result.
result = server.functions.multiplyAndDivide(20, 4)
print "Product:", result['product']
print "Quotient:", result['quotient']

PHP windows read write to/from serial port usind DOTNET System.IO.Ports

I try to capture data from a industrial scale weight serial port connection in my PHP webpage and print ticket.
Also I need to forward data to another serial port for display propose.
I explore anything from here and Google ...except very expensive ActiveXperts Serial Port SDK I cannot find anything more complex than PHP native fopen.
I found this here, but nothing happend...no error trigger nothing
$serial = new DOTNET('System', 'System.IO.Ports.SerialPort');
$serial->PortName = 'COM1';
$serial->Open();
Please give me a clue about how to implement this.
You could use
$serial = new DOTNET('System', 'System.IO.Ports.SerialPort');
$serial->PortName = 'COM1';
$serial->Open();
and then and
$serial->Write();
$serial->Read();
If there is no error is worth a try. You can also check out http://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx
Also you usually end up with that info if you are searching for windows com# communication with php. Check out:
https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CB0QFjAA&url=https%3A%2F%2Fgithub.com%2FXowap%2FPHP-Serial&ei=JXdSVKPsHYmGyQSftIL4Dw&usg=AFQjCNGxbGUeLotG_DvmUzDo2yBScDAqTw&sig2=AUp7IvRqKXesVZIF6Xf6MA
Its native php that suppossedly works in windows no complicated stuff and reconfigs needed

Get windows title using php doesn't work on browser call

My problem is I need to fetch FOOBAR2000's title because that including information of playing file, so I create a execute file via Win32 API(GetWindowText(), EnumWindows()) and it's working good.
TCHAR SearchText[MAX_LOADSTRING] = _T("foobar2000");
BOOL CALLBACK WorkerProc(HWND hwnd, LPARAM lParam)
{
TCHAR buffer[MAX_TITLESTRING];
GetWindowText(hwnd, buffer, MAX_TITLESTRING);
if(_tcsstr(buffer, SearchText))
{
// find it output something
}
return TRUE;
}
EnumWindows(WorkerProc, NULL);
Output would look like "album artis title .... [foobar2000 v1.1.5]"
I created a php file like test.php, and use exec() to execute it.
exec("foobar.exe");
then in console(cmd) I use command to execute it
php test.php
It's working good too, same output like before.
Now I use browser(firefox) to call this php file(test.php), strange things happened.
The output only foobar2000 v1.1.5, others information gone ...
I think maybe is exec() problem? priority or some limitation, so I use C# to create a COM Object and register it, and rewrite php code
$mydll = new COM("FOOBAR_COMObject.FOOBAR_Class");
echo $mydll->GetFooBarTitle();
still same result, command line OK, but browser Fail.
My question is
Why have 2 different output between command line and browser. I can't figure it out.
How can I get correct output via browser.
or there is a easy way to fetch FOOBAR2000's title?
Does anyone have experience on this problem?
== 2012/11/28 edited ==
follow Enno's opinion, I modify http_control plug-in to add filename info, original json info is "track title".
modify as following
state.cpp line 380 add 1 line
+pb_helper1 = pfc::string_filename(pb_item_ptr->get_path());
pb_helper1x = xml_friendly_string(pb_helper1);
# 1: when firefox opens the php and it gets executed, it the context depends on the user which runs the php-container (apache), this is quite different from the commandline call which gets executed in your context
# 2 and 3: there seems to be more than one way for getting the title: use the foobar-sdk and create a module which simply reads the current title per api, then write your result in an static-html-document inside your http-root-folder OR use the http-client inside the sdk, with it, you do not need a wabserver, even better use a already implemented module: for instance foo_upnp or foo-httpcontrol
Good luck!
If your webserver runs as a service, in windows you need to enable "allow desktop interaction" for the service. Your php script runs as a child of the webserver process when requested via browser.

Is it possible to get special Local Disk information from PHP?

I am running my PHP code on my local computer,So ,I just want to know the PHP has any functions to get the local hard disk information.Such as the disk name , disk space,free space available .etc.
Thank you very much!!
Builtins:
disk_free_space()
disk_total_space()
If you need more, you can access local commands via system().
For disk usage, you'll also find different snippets on the net, e.g. this:
http://justynshull.com/2010/08/du2-php/, which uses shell_exec().
In addition, yes, it is possible to retrieve this information, because you can execute commands. There are a few PHP functions to retrieve information about the disk as well, check out the following links:
disk_total_space( $directory );
disk_free_space( $directory );
Retrieving the disk's name, however, needs to be done with a command. I've just booted Windows in VirtualBox and it seems the following would work:
if( preg_match( '~Volumenaam : (.*)~i', `fsutil fsinfo volumeinfo C:\\`, $matches ) ) {
echo $matches[1];
}
Volumenaam is Dutch for "Volumename". I only have the Dutch version of Windows, so you'd have to check what the actual string is.
Good luck.
There are two functions: disk_free_space() and disk_total_space().
Did you try searching the PHP manual before you asked here?
These two functions came up very quickly for me:
disk_total_space()
disk_free_space()
Further data such as the disc name may be trickier (you may need to use system() as per another answer here), but that should give you a start.
However, note that when PHP is running in a web site, it will have quite a lot of restrictions on what it can do; it's likely to be restricted to its local web folders for any disc access.
For the Disk Name you can use this function
function GetVolumeLabel($drive) {
// Try to grab the volume name
if (preg_match('#Volume in drive [a-zA-Z]* is (.*)\n#i', shell_exec('dir '.$drive.':'), $m)) {
$volname = ' ('.$m[1].')';
} else {
$volname = '';
}
return $volname;
}
echo GetVolumeLabel("c");

Use DLL in PHP?

I'm not going to lie. I'm not all the familiar with Windows and COM objects. That's why i'm here. First of all is it possible to access a DLL from within a PHP script running out of Apache? In my journey around the internets i believe that i have 2 options:
compile the dll as an extension for PHP. (i didn't make this dll)
access the DLL as a COM object which is sort of what it's designed for anyways.
So i'm taking the COM approach.
try{
$com = new COM('WHAT_GOES_HERE');
} catch(Exception $e){
echo 'error: ' . $e->getMessage(), "\n";
}
How do i go about finding out what would go into the initialization string? is there a com viewer type program i could/should be using to find this out? the documentation associated with this DLL doesn't seem to specify what strings i should be using to initialize but gets very in-depth into what streams are available, and all sorts of fun stuff. just gotta past this initial hump. Please help!
WHAT_GOES_HERE is the ProgID, Class ID or Moniker registered on the Operating System.
Each of these can change for the same DLL registered on different machines. There are several ways to find what's the ProgID/CLSID/Moniker of a registered dll. You can search the web for "dll debugger", "dll export", "dll inspect" and you'll see several solutions, and also ways to show what functions the dll export so you can use them.
The easiest way, you can just register the dll with Regsvr32.exe and search Window's register with regedit.exe for the dll's name, you might need to search several times until you find the key under \HKEY_CLASSES_ROOT\, which is the ProgID.
The command dcomcnfg.exe shows a lot of information about COM objects.
If you have Visual Studio, the OLE/COM Object Viewer (oleview.exe) might be useful.
You can run dll functions (from dlls which are not php extensions) with winbinder.
http://winbinder.org/
Using it is simple. You have to download php_winbinder.dll and include it in php.ini as an extension.
In the php script you have to use something similar:
function callDll($func, $param = "")
{
static $dll = null;
static $funcAddr = null;
if ($dll === null)
{
$dll = wb_load_library(<DLL PATH AND FILENAME>);
}
$funcAddr = wb_get_function_address($func, $dll);
if ($param != "")
{
return wb_call_function($funcAddr,array(mb_convert_encoding($param,"UTF-16LE")));
}
else
{
return wb_call_function($funcAddr);
}
}
You can simply develop a wrapper around your main dll and use this wrapper as an extension in your PHP. Some free tools like SWIG can generate this wrapper for you automatically by getting the header of your dll functions. I myself use this approach and it was easy and reliable.
With PHP>=7.4.0's new FFI/Foreign Function Interface (which didn't exist yet when this question was posted), this is now easier than ever before! For example, to call the GetCurrentProcessId(); function from kernel32.dll:
<?php
declare(strict_types=1);
$ffi = FFI::cdef(
'unsigned long GetCurrentProcessId(void);',
"C:\\windows\\system32\\kernel32.dll"
);
var_dump($ffi->GetCurrentProcessId());
outputs
C:\PHP>php test.php
int(24200)
:)

Categories