System
[ class tree: System ] [ index: System ] [ all elements ]

Source for file WinDrives.php

Documentation is available at WinDrives.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Christian Weiske <cweiske@php.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: WinDrives.php,v 1.1 2005/07/10 07:29:39 cweiske Exp $
  20.  
  21.  
  22. require_once('PEAR.php');
  23.  
  24. if (!defined('SYSTEM_WINDRIVE_REMOVABLE')) {
  25.     define('SYSTEM_WINDRIVE_ERROR'    1);
  26.     define('SYSTEM_WINDRIVE_REMOVABLE'2);
  27.     define('SYSTEM_WINDRIVE_FIXED'    3);
  28.     define('SYSTEM_WINDRIVE_REMOTE'   4);
  29.     define('SYSTEM_WINDRIVE_CDROM'    5);
  30.     define('SYSTEM_WINDRIVE_RAMDISK'  6);
  31. }
  32.  
  33. /**
  34. * Get drive information on windows systems
  35. *
  36. * This class gives back a list of existing drives
  37. * (like a:\, c:\ and so) as well as the drive types
  38. * (hard disk, cdrom, network, removable)
  39. * and drive names if any
  40. *
  41. * The class requires the php_w32api.dll on php4 and
  42. * the php_ffi.dll for php5
  43. *
  44. * Note that the php_win32api.dll shipped with normal php
  45. * packages has a problem with many parameters
  46. * This means that the script will crash when trying
  47. * to get the drive name. use "setReadName(false)" to
  48. * prevent this.
  49. *
  50. * On php5, the drive _names_ are always ''.
  51. *   
  52. * You should not use this on non-Windows operating systems
  53. *
  54. * If you use this class in your projects, I ask you
  55. *   to send a real-world postcard to:
  56. *     Christian Weiske
  57. *     Dorfstrasse 42
  58. *     04683 Threna
  59. *     Germany
  60. *
  61. @author Christian Weiske <cweiske@php.net>
  62. @version 0.1
  63. @access public
  64. @package System
  65. */
  66. {
  67.     /**
  68.     * If the drive names shall be enumerated
  69.     * This can cause problems with some versions
  70.     * of win32api.dll, so it's disabled by default
  71.     * @access protected
  72.     * @var boolean 
  73.     */
  74.     var $bReadName = false;
  75.     
  76.     /**
  77.     * The win32api object to use
  78.     * If it's null, it can't be used
  79.     * @access protected
  80.     * @var object 
  81.     */
  82.     var $objApi   = null;
  83.     
  84.     /**
  85.     * The php_ffi object to use
  86.     * If it's null, it can't be used
  87.     * php_ffi replaces win32api in php5
  88.     * @access protected
  89.     * @var object 
  90.     */
  91.     var $objFFI   = null;
  92.     
  93.     /**
  94.     * List with titles for the drive types
  95.     * @access public
  96.     * @var array 
  97.     */
  98.     var $arTypeTitles = array(
  99.         'A' => '3.5" Floppy',
  100.         SYSTEM_WINDRIVE_ERROR     => 'non-existent',
  101.         SYSTEM_WINDRIVE_REMOVABLE => 'Removable',
  102.         SYSTEM_WINDRIVE_FIXED     => 'Harddisk',
  103.         SYSTEM_WINDRIVE_REMOTE    => 'Network drive',
  104.         SYSTEM_WINDRIVE_CDROM     => 'CD-Rom',
  105.         SYSTEM_WINDRIVE_RAMDISK   => 'RAM-Disk'
  106.     );
  107.     
  108.     
  109.     
  110.     /**
  111.     * Constructs the class and checks if the api
  112.     * is available
  113.     *
  114.     * @access public
  115.     * @param  boolean If the drive names shall be read
  116.     */
  117.     function System_WinDrives($bReadName = false)
  118.     {
  119.         if (version_compare(phpversion()'5.0.0''>=')) {
  120.             //PHP 5
  121.             if (class_exists('FFI'|| PEAR::loadExtension('ffi')) {
  122.                 //we've got the dll
  123.                 $strFuncs "[lib='kernel32.dll'] long GetLogicalDriveStringsA(long nBufferLength, char *lpBuffer);"
  124.                           . "[lib='kernel32.dll'] long GetLogicalDrives();"
  125.                           . "[lib='kernel32.dll'] int GetDriveTypeA(char *lpRootPathName);";
  126.                 $this->objFFI = new FFI($strFuncs);
  127.             }
  128.         else {
  129.             //PHP 4
  130.             if (class_exists('win32'|| PEAR::loadExtension('w32api')) {
  131.                 //we have the dll
  132.                 $this->objApi =new win32();
  133.                 $this->objApi->registerfunction("long GetLogicalDriveStrings Alias GetLogicalDriveStrings (long &BufferLength, string &Buffer) From kernel32.dll");
  134.                 $this->objApi->registerfunction("long GetLogicalDrives Alias GetLogicalDrives () From kernel32.dll");
  135.                 $this->objApi->registerfunction("int GetDriveType Alias GetDriveType (string lpRootPathName) From kernel32.dll");
  136.                 $this->objApi->registerfunction("long GetVolumeInformationA Alias GetVolumeInformation (string lpRootPathName, string &lpVolumeNameBuffer, int nVolumeNameSize, int &lpVolumeSerialNumber, int &lpMaximumComponentLength, int &lpFileSystemFlags, string &lpFileSystemNameBuffer, int nFileSystemNameSize) From kernel32.dll")
  137.             }
  138.         }
  139.         $this->setReadName($bReadName);
  140.     }
  141.     
  142.     
  143.     
  144.     /**
  145.     * returns an array containing information about all drives
  146.     *
  147.     * @access public
  148.     * @return array   Array with drive infomation
  149.     */
  150.     function getDrivesInformation()
  151.     {
  152.         $arInfo = array();
  153.         $arDrives $this->getDriveList();
  154.         foreach ($arDrives as $strDrive{
  155.             $arInfo[$strDrive]->type = $this->getDriveType($strDrive);
  156.             $arInfo[$strDrive]->name = $this->getDriveName($strDrive);
  157.             $arInfo[$strDrive]->typetitle = $this->getTypeTitle($arInfo[$strDrive]->type$strDrive);
  158.         }
  159.         return $arInfo;
  160.     }
  161.     
  162.     
  163.     
  164.     /**
  165.     * Setter for "bReadName"
  166.     *
  167.     * @access public
  168.     * @param boolean  If the drive's names shall be read
  169.     */
  170.     function setReadName($bReadName)
  171.     {
  172.         $this->bReadName = $bReadName;
  173.     }
  174.     
  175.     
  176.     
  177.     /**
  178.     * return the "bReadName" setting
  179.     *
  180.     * @access public
  181.     */
  182.     function getReadName()
  183.     {
  184.         return $this->bReadName;
  185.     }
  186.     
  187.     
  188.     
  189.     /**
  190.     * checks if the win32 api/ffi is available
  191.     *
  192.     * @access public
  193.     * @return boolean True if the api can be used, false if the dll is missing
  194.     */
  195.     function isApiAvailable()
  196.     {
  197.         return ($this->objApi !== null || $this->objFFI !== null);
  198.     }
  199.     
  200.  
  201.         
  202.     /**
  203.     * returns a list with all drive paths
  204.     * like "A:\", "C:\" and so
  205.     *
  206.     * @access public
  207.     * @return array   Array with all drive paths
  208.     */
  209.     function getDriveList()
  210.     {
  211.         $arDrives = array();
  212.         
  213.         if ($this->objApi !== null{
  214.             //set the length your variable should have
  215.             $len = 105;
  216.             //prepare an empty string
  217.             $buffer str_repeat("\0"$len + 1);
  218.             
  219.             if ($this->objApi->GetLogicalDriveStrings($len$buffer)) {
  220.                 $arDrives explode("\0"trim($buffer));
  221.             elseif ($drive_list $this->objApi->GetLogicalDrives()) {
  222.                 $arDrives $this->splitDriveNumner($drive_list);
  223.             }
  224.         else if ($this->objFFI !== null{
  225.             $drive_list $this->objFFI->GetLogicalDrives();
  226.             $arDrives   $this->splitDriveNumber($drive_list);
  227.         }
  228.         
  229.         if (count($arDrives== 0{
  230.             //nothing found... we'll guess
  231.             $arDrives $this->guessDriveList();
  232.         }
  233.         
  234.         return $arDrives;
  235.     }
  236.     
  237.     
  238.     
  239.     /**
  240.     * splits a number returned by GetLogicalDrives*()
  241.     * into an array with drive strings ("A:\", "C:\")
  242.     *
  243.     * @access protected
  244.     * @param  int     The number the function gave back
  245.     * @return array   Array with drives
  246.     */
  247.     function splitDriveNumber($nDrives)
  248.     {
  249.         $arDrives = array();
  250.         //A=65, Z=90
  251.         for ($i=1$drv = 65; $drv <= 90; $drv++{
  252.             if ($nDrives $i{
  253.                 $arDrives[chr($drv":\\";
  254.             }
  255.             $i $i << 1;
  256.         }
  257.         return $arDrives;
  258.     }
  259.     
  260.     
  261.     
  262.     /**
  263.     * Tries to guess the drive list
  264.     * The floppy "A:\" will no be in the list
  265.     *
  266.     * @access public
  267.     * @return array   Array with all the drive paths like "A:\" and "C:\"
  268.     */
  269.     function guessDriveList()
  270.     {
  271.         $arDrives   = array();
  272.         //DON'T begin with A or B as a message box will pop up
  273.         //if no floppy is provided
  274.         //A=65, C=67, Z=90
  275.         for ($i = 1$drv = 67; $drv <= 90; $drv++{
  276.             if (is_dir(chr($drv':\\')) {
  277.                 $arDrives[chr($drv':\\';
  278.             }
  279.         }
  280.         return $arDrives;
  281.     }
  282.     
  283.     
  284.     
  285.     /**
  286.     * returns the drive type
  287.     *
  288.     * @access public
  289.     * @param  string  Drive path like "C:\"
  290.     * @return int     Drive type, use "DRIVE_*" constants to enumerate it
  291.     */
  292.     function getDriveType($strDrive)
  293.     {
  294.         if ($this->objApi !== null{
  295.             $nType $this->objApi->GetDriveType($strDrive);
  296.         else if ($this->objFFI !== null{
  297.             $nType $this->objFFI->GetDriveTypeA($strDrive);
  298.         else {
  299.             //no api available...
  300.             $nType SYSTEM_WINDRIVE_FIXED;
  301.         }
  302.         return $nType;
  303.     }
  304.     
  305.     
  306.     
  307.     /**
  308.     * returns the title for a given drive type
  309.     *
  310.     * @access public
  311.     * @param  int     The drive type
  312.     * @param  string  The drive path (like "A:\")
  313.     * @return string  The type title like "Harddisk"
  314.     */
  315.     function getTypeTitle($nType$strDrive)
  316.     {
  317.         if ($nType == SYSTEM_WINDRIVE_REMOVABLE && $strDrive[0== 'A'{
  318.             $nType 'A';
  319.         }
  320.         return $this->arTypeTitles[$nType];
  321.     }
  322.     
  323.     
  324.     
  325.     /**
  326.     * returns the name of the drive
  327.     *
  328.     * The name is the one the user has given the drive
  329.     * like "Windows" or "Data"
  330.     *
  331.     * The FFI version doesn't seem to work...
  332.     * $strFunc .= "[lib='kernel32.dll'] long GetVolumeInformationA(char lpRootPathName, char *lpVolumeNameBuffer, int nVolumeNameSize, "
  333.         . "long *lpVolumeSerialNumber, long *lpMaximumComponentLength, long *lpFileSystemFlags, char *lpFileSystemNameBuffer, int nFileSystemNameSize);";
  334.     *
  335.     * @access public
  336.     * @param  string  Drive path like "C:\"
  337.     * @return string  The name of the drive
  338.     */
  339.     function getDriveName($strDrive)
  340.     {
  341.         if ($this->objApi === null || $this->bReadName === false{
  342.             return '';
  343.         }
  344.         
  345.         $strName                '';
  346.         $serialNo               = 0; 
  347.         $MaximumComponentLength = 0; 
  348.         $FileSystemFlags        = 0; 
  349.         $VolumeNameSize         = 260; 
  350.         $VolumeNameBuffer       str_repeat("\0"$VolumeNameSize)
  351.         $FileSystemNameSize     = 260; 
  352.         $FileSystemNameBuffer   str_repeat("\0"$FileSystemNameSize)
  353.         if ($result $this->objApi->GetVolumeInformation
  354.             $strDrive$VolumeNameBuffer$VolumeNameSize
  355.             $serialNo$MaximumComponentLength
  356.             $FileSystemFlags$FileSystemNameBuffer$FileSystemNameSize)) {
  357.             $strName trim($VolumeNameBuffer);
  358.         }
  359.         
  360.         return $strName;
  361.     }
  362. }//class System_WinDrives
  363. ?>

Documentation generated on Mon, 11 Mar 2019 14:48:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.