logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> How-to's & Documentation & Tips View previous topic :: View next topic
Reply to topic   Topic: Use VBS in PHP (Win32)
Author
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7288
Location: Germany, Next to Hamburg

PostPosted: Wed 22 Mar '06 14:00    Post subject: Use VBS in PHP (Win32) Reply with quote

For many tasks of an admin vbs is a usefull tool. But VBS runs only locale. (Ok, access from outside is possible but not easy)
So I tried to implement vbs into PHP to access this from the web.

PHP has the COM API. With that we can access Windows resources.
Here an example to check the freespace from all devices.
What is the device? (CD, HDD, etc)
How much freespace?
Totalsize?



Sorry, the inline documentation is in german. If you have any questions, ask here

The original vbs code

Code:

'v2.9*****************************************************
' File:   LaufWerkListe.vbs
' Autor:  dieseyer@gmx.de 
'         dieseyer.de
'
' Listet alle lokalen Laufwerke auf & erstellt Log-Datei.
'*********************************************************
 
Option Explicit
 
Dim WshShell, fso, FileOut, DriveList, i, Text1, Text2, Text3
 
Set WshShell  = WScript.CreateObject("WScript.Shell")
Set fso       = CreateObject("Scripting.FileSystemObject")
Set DriveList = fso.Drives
 
' Protokoll in Datei schreiben
Set FileOut = fso.OpenTextFile(WScript.ScriptName & ".log", 8, true)        ' Datei zum Schreiben öffnen (notfals anlegen)
fileOut.WriteLine(vbCRLF & now() & " Protokoll von " & WScript.ScriptName & ": ")   
 
   For Each i in DriveList 
      if 0  = i.DriveType Then  Text1  = "???     " & vbTab & i.DriveLetter & ": " & vbTab
      if 1  = i.DriveType Then  Text1  = "Disk-Lw." & vbTab & i.DriveLetter & ": " & vbTab
      if 2  = i.DriveType Then  Text1  = "Festpl. " & vbTab & i.DriveLetter & ": " & vbTab
      if 3  = i.DriveType Then  Text1  = "Netz-Lw." & vbTab & i.DriveLetter & ": " & vbTab
      if 4  = i.DriveType Then  Text1  = "CD-Lw.  " & vbTab & i.DriveLetter & ": " & vbTab
      if 5  = i.DriveType Then  Text1  = "RAM-Lw. " & vbTab & i.DriveLetter & ": " & vbTab
      If i.IsReady Then
 
              Text3 = ""
              Text3 =      FormatNumber(i.FreeSpace/1024/1024, 1)  & "MB" & vbTab & "von" & vbTab
          if Text3 <> "" then Text1 = Text1 & Text3
          if Text3  = "" then Text1 = Text1 & "?-?-?MB" & vbTab & "von" & vbTab
              Text3 = ""
              Text3 =      FormatNumber(i.TotalSize/1024/1024, 1)  & "MB" & vbTab & " frei"
          if Text3 <> "" then Text1 = Text1 & Text3
          if Text3  = "" then Text1 = Text1 & "?-?-?MB" & vbTab  & " frei"
 
      End If     
 
   fileOut.WriteLine(Text1)   
      Text2 = Text2 & Text1 & vbCRLF
   Next
    FileOut.Close
Set FileOut = Nothing
 
MsgBox Text2, , WScript.ScriptName


Now converted into PHP
Code:

<?php
/**
* VBS Parts from dieseyer.de => LaufWerkListe.vbs
*/
/**
* wahre Datei Größe ermitteln
* @param $filesize Dateigröße in Bytes filesize($value)
* @return gedrundete $filesize
*/
function getRealSize ($filesize){
    if(($filesize/1024)<1024){
        return round (($filesize/1024),2) . " kb";
    }
    elseif((($filesize/1024)/1024)<1024){
        return round ((($filesize/1024)/1024),2) . " Mb";     
    }
    elseif(((($filesize/1024)/1024)/1024)<1024)
    {
        return round (((($filesize/1024)/1024)/1024),2) . " Gb";
    }
    else
    {
        return round ((((($filesize/1024)/1024)/1024)/1024),2) . " Tb";
    }
}

$fso=new COM("Scripting.FileSystemObject");
$drives=$fso->Drives;

echo "<table border=\"1\">";

foreach($drives as $drive){
    if($drive->DriveType=="0"){$typ="???";}
    if($drive->DriveType=="1"){$typ="Disk-Lw.";}
    if($drive->DriveType=="2"){$typ="Festpl.";}
    if($drive->DriveType=="3"){$typ="Netz-Lw.";}
    if($drive->DriveType=="4"){$typ="CD-Lw.";}
    if($drive->DriveType=="5"){$typ="RAM-Lw.";}
    if(($drive->DriveType!="1")&&($drive->DriveType!="4")){
        $free=$drive->FreeSpace;
        $free=getRealSize($free);
        $size=$drive->TotalSize;
        $size=getRealSize($size);
    }
    else
    {
        $free="n/a";
        $size="n/a";
    }
    echo "<tr><td>". $drive ."</td><td>". $typ . "</td><td>Größe: ". $size ."</td><td>Frei: ".$free ."</td></tr>";
}
echo "</table>";     
?>


You see, it is not hard to get the code from vbs into PHP.
For security you may set a .htaccess.
I'd like to see your scripts here!
Good luck
Back to top


Reply to topic   Topic: Use VBS in PHP (Win32) View previous topic :: View next topic
Post new topic   Forum Index -> How-to's & Documentation & Tips