home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / Folders.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  19.7 KB  |  711 lines

  1. <?php
  2. /**
  3. *   Provides the locations of several system and user directories
  4. *   independent of the operating system used.
  5. *
  6. *   Simpe example:
  7. *       require_once 'System/Folders.php';
  8. *       $sf = new System_Folders();
  9. *       echo $sf->getHome();
  10. *
  11. *   If you want the folders to be cached (not re-calculated on every
  12. *    read), use System_Folders_Cached.
  13. *
  14. *   @category   System
  15. *   @package    System_Folders
  16. *   @author     Christian Weiske <cweiske@php.net>
  17. *   @license    LGPL
  18. *   @version    CVS: $Id: Folders.php,v 1.2 2006/03/18 07:19:29 cweiske Exp $
  19. */
  20.  
  21. require_once 'OS/Guess.php';
  22.  
  23. if (!defined('SYS_LINUX')) {
  24.     define('SYS_LINUX'  , 'linux');
  25.     define('SYS_WINDOWS', 'windows');
  26.     define('SYS_MAC'    , 'darwin');
  27. }
  28.  
  29. /**
  30. *   Provides the locations of several system and user directories
  31. *   independent of the operating system used.
  32. *
  33. *   If a path does not exist or can't be found (error), NULL is returned.
  34. *
  35. *   The class uses both $_ENV and $_SERVER to retrieve the environment
  36. *    paths, as this seems to be different between php4 and 5.
  37. *
  38. *   @category   System
  39. *   @package    System_Folders
  40. *   @author     Christian Weiske <cweiske@php.net>
  41. */
  42. class System_Folders
  43. {
  44.     /**
  45.     *   The operating system on which
  46.     *   we work here
  47.     *   Gotten from OS_Guess::getSysname()
  48.     *
  49.     *   Values (are lowercase):
  50.     *   - windows
  51.     *   - linux
  52.     *   - darwin
  53.     *
  54.     *   use the SYS_* constants to check it
  55.     *
  56.     *   @var string
  57.     *   @access protected
  58.     */
  59.     var $sys = 'unknown';
  60.  
  61.     /**
  62.     *   Known names for the application directory
  63.     *   in windows.
  64.     *
  65.     *   @var array
  66.     *   @access protected
  67.     */
  68.     var $arAppDataNames = array(
  69.         'Application data',       //english
  70.         'Anwendungsdaten',        //german
  71.         'Toepassingsgegevens',    //dutch
  72.         'Datos de programa',      //spanish
  73.         'Dados de aplicativos',   //portugese
  74.         'Data aplikac├¡',          //czech
  75.         'Programdata',            //norwegian
  76.         'Henkilokohtainen',       //finnish
  77.         'Donnees d\'applications',//french
  78.         'Dati applicazioni',      //italian
  79.         'Dane aplikacji',         //polish
  80.     );
  81.  
  82.  
  83.     /**
  84.     *   Known names for the my documents directory
  85.     *   on windows.
  86.     *
  87.     *   @var array
  88.     *   @access protected
  89.     */
  90.     var $arDocumentsWindows = array(
  91.         'My Documents',   //english
  92.         'Own Files',      //english?
  93.         'Eigene Dateien', //german
  94.         'Documenti',      //italian
  95.     );
  96.  
  97.  
  98.     /**
  99.     *   Known names for the my Desktop directory.
  100.     *
  101.     *   @var array
  102.     *   @access protected
  103.     */
  104.     var $arDesktop = array(
  105.         'Desktop' //english, german, italian
  106.     );
  107.  
  108.  
  109.  
  110.     /**
  111.     *   Known names for the my documents directory
  112.     *   on linux and mac.
  113.     *
  114.     *   @var array
  115.     *   @access protected
  116.     */
  117.     var $arDocumentsLinux = array(
  118.         'Documents',    //english
  119.         'Dokumente',    //german
  120.         'Documenti',    //italian
  121.     );
  122.  
  123.  
  124.     /**
  125.     *   Known paths for the documents and settings directory
  126.     *   on windows.
  127.     *
  128.     *   @var array
  129.     *   @access protected
  130.     */
  131.     var $arDocsAndSettings = array(
  132.         'C:\\Documents and Settings\\',     //english, italian
  133.         'C:\\Dokumente und Einstellungen\\' //german
  134.     );
  135.  
  136.  
  137.     /**
  138.     *   Known paths for the programs directory on windows.
  139.     *
  140.     *   @var array
  141.     *   @access protected
  142.     */
  143.     var $arProgramsWindows = array(
  144.         'C:\\Program Files\\',
  145.         'C:\\Programs\\',
  146.         'C:\\Programme\\',     //german
  147.         'C:\\Programmi\\',     //italian
  148.     );
  149.  
  150.     /**
  151.     *   Known names for the shared documents directory
  152.     *   on windows.
  153.     *   Although the explorer shows "Shared documents"
  154.     *   or "Gemeinsame Dokumente", the *real* directory
  155.     *   is only one of the ones here.
  156.     *
  157.     *   @var array
  158.     *   @access protected
  159.     */
  160.     var $arSharedDocumentsWindows = array(
  161.         'Documents', //english
  162.         'Dokumente', //german
  163.         'Documenti', //italian
  164.     );
  165.  
  166.     /**
  167.     *   Known paths for the windows directory.
  168.     *
  169.     *   @var array
  170.     *   @access protected
  171.     */
  172.     var $arWindowsDirs = array(
  173.         'C:\\WINDOWS\\',
  174.         'C:\\WINNT\\',
  175.         'C:\\WIN98\\',
  176.         'C:\\WIN95\\',
  177.         'C:\\WIN2000\\',
  178.         'C:\\WIN2K\\',
  179.         'C:\\WINXP\\'
  180.     );
  181.  
  182.     /**
  183.     *   COM object used in Windows to get
  184.     *   special folder locations via SpecialFolders()
  185.     *   described in
  186.     *   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/14761fa3-19be-4742-9f91-23b48cd9228f.asp
  187.     *
  188.     *   Available folders:
  189.     *   AllUsersDesktop
  190.     *   AllUsersStartMenu
  191.     *   AllUsersPrograms
  192.     *   AllUsersStartup
  193.     *   Desktop
  194.     *   Favorites
  195.     *   Fonts
  196.     *   MyDocuments
  197.     *   NetHood
  198.     *   PrintHood
  199.     *   Programs
  200.     *   Recent
  201.     *   SendTo
  202.     *   StartMenu
  203.     *   Startup
  204.     *   Templates
  205.     *
  206.     *   If this variable is NULL, it hasn't been created yet.
  207.     *   If it is FALSE, it cannot be created and used (e.g.
  208.     *       because COM is not available)
  209.     *
  210.     *   @var COM
  211.     *   @access protected
  212.     */
  213.     var $objCom = null;
  214.  
  215.  
  216.  
  217.     /**
  218.     *   Constructor; initializes the system
  219.     *   variable.
  220.     */
  221.     function System_Folders()
  222.     {
  223.         $og = new OS_Guess();
  224.         $this->sys = $og->getSysname();
  225.     }//function System_Folders()
  226.  
  227.  
  228.  
  229.     /**
  230.     *   Adds a trailing slash to the given path if there is none.
  231.     *   Uses DIRECTORY_SEPARATOR, so it works with windows and *nix.
  232.     *
  233.     *   @param  string  The path
  234.     *   @return string  The path with a trailing slash
  235.     *   @access protected
  236.     */
  237.     function addTrailingSlash($strPath)
  238.     {
  239.         if ($strPath === null) {
  240.             return $strPath;
  241.         }
  242.         if (substr($strPath, -1) !== DIRECTORY_SEPARATOR) {
  243.             $strPath .= DIRECTORY_SEPARATOR;
  244.         }
  245.         return $strPath;
  246.     }//function addTrailingSlash($strPath)
  247.  
  248.  
  249.  
  250.     /**
  251.     *   Directories in windows environment variables sometimes
  252.     *   have a double backslash, and this needs to be fixed.
  253.     *
  254.     *   @param  string  The path
  255.     *   @return string  The fixed path
  256.     *   @access protected
  257.     */
  258.     function fixWindowsPath($strPath)
  259.     {
  260.         if ($strPath === null) {
  261.             return null;
  262.         }
  263.         return str_replace('\\\\', '\\', $strPath);
  264.     }//function fixWindowsPath($strPath)
  265.  
  266.  
  267.  
  268.     /**
  269.     *   Loops through a list of given paths and checks
  270.     *   which of them are correct.
  271.     *
  272.     *   @param array    $arPaths        Array with paths to test
  273.     *   @param string   $strBase        Base directory that shall be prepended to all paths
  274.     *   @param string   $strSuffix      String appended to the directory path
  275.     *   @return string      The directory that exists. NULL if none of them matched.
  276.     *   @access protected
  277.     *   @static
  278.     */
  279.     function tryPaths($arPaths, $strBase = '', $strSuffix = '')
  280.     {
  281.         foreach ($arPaths as $strName) {
  282.             $strTmp = $strBase . $strName . $strSuffix;
  283.             if (file_exists($strTmp) && is_dir($strTmp)) {
  284.                 return $strTmp;
  285.             }
  286.         }
  287.  
  288.         return null;
  289.     }//function tryPaths($arPaths, $strBase = '', $strSuffix = '')
  290.  
  291.  
  292.  
  293.     /**
  294.     *   Loads the COM object into the $objCom variable.
  295.     *
  296.     *   @return boolean     true if it could be loaded, false if not
  297.     *   @access protected
  298.     */
  299.     function loadCOM()
  300.     {
  301.         //prevent double-loading
  302.         if ($this->objCom !== null) {
  303.             return $this->objCom !== false;
  304.         }
  305.  
  306.         if (!class_exists('COM')) {
  307.             $this->objCom = false;
  308.             return false;
  309.         }
  310.         $this->objCom = new COM('WScript.Shell');
  311.         if (!$this->objCom) {
  312.             $this->objCom = false;
  313.             return false;
  314.         }
  315.         return true;
  316.     }//function loadCOM()
  317.  
  318.  
  319.  
  320.     /**
  321.     *   Loads a windows path via COM using $objCom.
  322.     *
  323.     *   @param string   $strType    See $objCom for allowed values.
  324.     *   @return mixed   false if no path could be obtained, string otherwise
  325.     *   @access protected
  326.     */
  327.     function getCOMPath($strType)
  328.     {
  329.         if (!$this->loadCOM()) {
  330.             return false;
  331.         }
  332.         $strPath = $this->objCom->SpecialFolders($strType);
  333.         if (!$strPath || $strPath == '') {
  334.             return false;
  335.         } else {
  336.             return $strPath;
  337.         }
  338.     }//function getCOMPath($strType)
  339.  
  340.  
  341.  
  342.     /**
  343.     *   Returns the All Users directory.
  344.     *   Works on windows only, returns NULL if not found.
  345.     *
  346.     *   @return string  The all users directory
  347.     *   @access public
  348.     */
  349.     function getAllUsers()
  350.     {
  351.         if ($this->sys == SYS_WINDOWS) {
  352.             $arEnv = $_SERVER + $_ENV;
  353.             if (isset($arEnv['ALLUSERSPROFILE']) && is_dir($arEnv['ALLUSERSPROFILE'])) {
  354.                 return $this->addTrailingSlash($arEnv['ALLUSERSPROFILE']);
  355.             } else {
  356.                 $strDocsAndSettings = System_Folders::tryPaths($this->arDocsAndSettings);
  357.                 if ($strDocsAndSettings !== null) {
  358.                     $strAll = $strDocsAndSettings . 'All Users';
  359.                     if (is_dir($strAll)) {
  360.                         return $this->addTrailingSlash($strAll);
  361.                     }
  362.                 }
  363.             }
  364.         }
  365.         return null;
  366.     }//function getAllUsers()
  367.  
  368.  
  369.  
  370.     /**
  371.     *   Returns the path to the application data directory.
  372.     *   This is the directory in which applications save their
  373.     *   settings.
  374.     *
  375.     *   On Windows, this is an own directory called "Application data",
  376.     *   on *nix, the home directory is used.
  377.     *   MacOS X has two application settings directories:
  378.     *    - $HOME/Library/Preferences/<pref_file> for normal settings
  379.     *    - $HOME/Library/Application Support/<app_name>/ for data files that
  380.     *           that the application needs to store
  381.     *   This method returns the latter, as it works for both storing just
  382.     *    prefs and files in an application specific subdir. That's not 100%
  383.     *    correct for apple, but it will work.
  384.     *
  385.     *   @return string  The application data directory
  386.     *   @access public
  387.     */
  388.     function getAppData()
  389.     {
  390.         $strAppData = null;
  391.         if ($this->sys == SYS_WINDOWS) {
  392.             /**
  393.             *   win2k/xp: user_dir/names
  394.             *   win98: C:\windows\Anwendungsdaten|appdata
  395.             */
  396.             $strHome = $this->getHome();
  397.             $strAppData = System_Folders::tryPaths($this->arAppDataNames, $strHome);
  398.             if ($strAppData == null) {
  399.                 /**
  400.                 *   We didn't find it in the user directory,
  401.                 *   so check the windows dir - win9x had the
  402.                 *   data there
  403.                 */
  404.                 $strWindows = $this->getWindows();
  405.                 $strAppData = System_Folders::tryPaths($this->arAppDataNames, $strWindows);
  406.             }//appdata still null
  407.         } else {
  408.             $strAppData = $this->getHome();
  409.         }
  410.  
  411.         return $this->addTrailingSlash($strAppData);
  412.     }//function getAppData()
  413.  
  414.  
  415.  
  416.     /**
  417.     *   Returns the path to the user's desktop.
  418.     *
  419.     *   @return string The user's desktop
  420.     *   @access public
  421.     */
  422.     function getDesktop()
  423.     {
  424.         $strDesktop   = null;
  425.  
  426.         if ($this->sys == SYS_WINDOWS) {
  427.             $strDesktop = $this->getCOMPath('Desktop');
  428.             if ($strDesktop !== false && file_exists($strDesktop)) {
  429.                 return $this->addTrailingSlash($this->fixWindowsPath($strDesktop));
  430.             }
  431.         }
  432.  
  433.         $strHome      = $this->getHome();
  434.         if ($strHome === null) {
  435.             return null;
  436.         }
  437.  
  438.         $strDesktop = System_Folders::tryPaths($this->arDesktop, $strHome);
  439.  
  440.         return $this->addTrailingSlash($strDesktop);
  441.     }//function getDesktop()
  442.  
  443.  
  444.  
  445.     /**
  446.     *   Returns the path to the user's documents directory.
  447.     *   (normally below the home folder)
  448.     *
  449.     *   @return string The "documents" directory
  450.     *   @access public
  451.     */
  452.     function getDocuments()
  453.     {
  454.         $strDocuments = null;
  455.  
  456.         if ($this->sys == SYS_WINDOWS) {
  457.             $strDocuments = $this->getCOMPath('MyDocuments');
  458.             if ($strDocuments !== false && file_exists($strDocuments)) {
  459.                 return $this->addTrailingSlash($this->fixWindowsPath($strDocuments));
  460.             }
  461.             $arKnownNames = $this->arDocumentsWindows;
  462.         } else {
  463.             $arKnownNames = $this->arDocumentsLinux;
  464.         }
  465.  
  466.         $strHome = $this->getHome();
  467.         if ($strHome === null) {
  468.             return null;
  469.         }
  470.         $strDocuments = System_Folders::tryPaths($arKnownNames, $strHome);
  471.  
  472.         return $this->addTrailingSlash($strDocuments);
  473.     }//function getDocuments()
  474.  
  475.  
  476.  
  477.     /**
  478.     *   Returns the path to the user's home directory.
  479.     *
  480.     *   @return string The user's home directory
  481.     *   @access public
  482.     */
  483.     function getHome()
  484.     {
  485.         $strHome = null;
  486.         if ($this->sys == SYS_LINUX) {
  487.             if (isset($_ENV['HOME'])) {
  488.                 //environment variable set
  489.                 $strHome = $_ENV['HOME'];
  490.             } else {
  491.                 //env not set, so try the default directory
  492.                 $strUser = $this->getUserName();
  493.                 if ($strUser !== null) {
  494.                     $strHome = '/home/' . $strUser;
  495.                 }
  496.             }
  497.         } else if ($this->sys == SYS_MAC) {
  498.             if (isset($_ENV['HOME'])) {
  499.                 //environment variable set
  500.                 $strHome = $_ENV['HOME'];
  501.             } else {
  502.                 //env not set, so try the default directory
  503.                 $strUser = $this->getUserName();
  504.                 if ($strUser !== null) {
  505.                     $strHome = '/Users/' . $strUser;
  506.                 }
  507.             }
  508.         } else if ($this->sys == SYS_WINDOWS) {
  509.             $arEnv = $_SERVER + $_ENV;
  510.             if (isset($arEnv['USERPROFILE'])) {
  511.                 $strHome = $arEnv['USERPROFILE'];
  512.             } else if (isset($arEnv['HOMEPATH']) && isset($arEnv['HOMEDRIVE'])) {
  513.                 $strHome = $arEnv['HOMEDRIVE'] . $arEnv['HOMEPATH'];
  514.             } else {
  515.                 //guess it...
  516.                 $strUser = $this->getUserName();
  517.                 if ($strUser !== null) {
  518.                     //It seems as if the german version of windows is the only
  519.                     //one that translated "docs and settings". All other languages
  520.                     //use the english name
  521.                     $strHome = System_Folders::tryPaths($this->arDocsAndSettings) . $strUser;
  522.                 }
  523.             }
  524.             $strHome = $this->fixWindowsPath($strHome);
  525.         }//windows
  526.  
  527.         return $this->addTrailingSlash($strHome);
  528.     }//function getHome()
  529.  
  530.  
  531.  
  532.     /**
  533.     *   Returns the path to the programs directory.
  534.     *   This is the dir where all programs are installed
  535.     *   normally.
  536.     *
  537.     *   On windows, it's mostly "C:\Programs\", on linux,
  538.     *   the /opt/ directory is returned.
  539.     *
  540.     *   @return string  The programs directory
  541.     *   @access public
  542.     */
  543.     function getPrograms()
  544.     {
  545.         $strPrograms = null;
  546.  
  547.         if ($this->sys == SYS_LINUX) {
  548.             if (file_exists('/opt/') && is_dir('/opt/')) {
  549.                 $strPrograms = '/opt/';
  550.             }
  551.         } else if ($this->sys == SYS_MAC) {
  552.             $strPrograms = '/Applications/';
  553.         } else if ($this->sys == SYS_WINDOWS) {
  554.             $strPrograms = $this->getCOMPath('Programs');
  555.             if ($strPrograms === false || !file_exists($strPrograms)) {
  556.                 $arEnv = $_SERVER + $_ENV;
  557.                 if (isset($arEnv['ProgramFiles'])) {
  558.                     $strPrograms = $arEnv['ProgramFiles'];
  559.                 } else {
  560.                     //guess it
  561.                     $strPrograms = System_Folders::tryPaths($this->arProgramsWindows);
  562.                 }//guess it
  563.             }
  564.             $strPrograms = $this->fixWindowsPath($strPrograms);
  565.         }//windows
  566.  
  567.         return $this->addTrailingSlash($strPrograms);
  568.     }//function getPrograms()
  569.  
  570.  
  571.  
  572.     /**
  573.     *   Returns the path to the directory for temporary files.
  574.     *
  575.     *   @return string  The temporary directory
  576.     *   @access public
  577.     */
  578.     function getTemp()
  579.     {
  580.         $strTemp = null;
  581.  
  582.         if ($this->sys == SYS_LINUX || $this->sys == SYS_MAC) {
  583.             if (file_exists('/tmp/') && is_dir('/tmp/')) {
  584.                 $strTemp = '/tmp/';
  585.             }
  586.         } else if ($this->sys == SYS_WINDOWS) {
  587.             $arEnv = $_SERVER + $_ENV;
  588.             if (isset($arEnv['TEMP'])) {
  589.                 $strTemp = $arEnv['TEMP'];
  590.             } else if (isset($arEnv['TMP'])) {
  591.                 $strTemp = $arEnv['TMP'];
  592.             } else {
  593.                 //guess it
  594.                 $strTemp = System_Folders::tryPaths($this->arWindowsDirs, '', '\\Temp');
  595.             }//no env variable
  596.             $strTemp = $this->fixWindowsPath($strTemp);
  597.         }//windows
  598.  
  599.         return $this->addTrailingSlash($strTemp);
  600.     }//function getTemp()
  601.  
  602.  
  603.  
  604.     /**
  605.     *   Returns the path to the shared documents directory.
  606.     *
  607.     *   Supports windows only (at least for now) as no other
  608.     *   operating system seems to have such a folder.
  609.     *   Returns NULL on failure (not windows or not found).
  610.     *
  611.     *   @return string      The shared documents dir
  612.     *   @access public
  613.     */
  614.     function getSharedDocuments()
  615.     {
  616.         $strShared = null;
  617.         if ($this->sys == SYS_WINDOWS) {
  618.             $strAll = $this->getAllUsers();
  619.             if ($strAll !== null) {
  620.                 $strShared = System_Folders::tryPaths($this->arSharedDocumentsWindows, $strAll);
  621.             }
  622.         }
  623.  
  624.         return $this->addTrailingSlash($strShared);
  625.     }//function getSharedDocuments()
  626.  
  627.  
  628.  
  629.     /**
  630.     *   Returns the name of the guesses system.
  631.     *   Can be compared with SYS_* constants.
  632.     *
  633.     *   @return string  The detected system
  634.     *   @access public
  635.     */
  636.     function getSys()
  637.     {
  638.         return $this->sys;
  639.     }//function getSys()
  640.  
  641.  
  642.  
  643.     /**
  644.     *   Returns the name for the user
  645.     *   under which name the program runs.
  646.     *
  647.     *   This function returns the *system user name*,
  648.     *   not the name with forename and surname
  649.     *   On unix, this would be e.g. 'fbar' or so for
  650.     *   the user 'Foo Bar'
  651.     *
  652.     *   This method is used my most other methods, so
  653.     *   recognizing the user name is really important.
  654.     *   Be sure to check this method if the others fail.
  655.     *
  656.     *   @return string  The user name
  657.     *   @access public
  658.     */
  659.     function getUserName()
  660.     {
  661.         $strUser = null;
  662.         if ($this->sys == SYS_LINUX) {
  663.             if (isset($_ENV['USER'])) {
  664.                 $strUser = $_ENV['USER'];
  665.             } else {
  666.                 $strUser = trim(`whoami`);
  667.             }
  668.         } else if ($this->sys == SYS_MAC) {
  669.             if (isset($_ENV['USER'])) {
  670.                 $strUser = $_ENV['USER'];
  671.             }
  672.         } else if ($this->sys == SYS_WINDOWS) {
  673.             $arEnv = $_SERVER + $_ENV;
  674.             if (isset($arEnv['USERNAME'])) {
  675.                 $strUser = $arEnv['USERNAME'];
  676.             }
  677.         }
  678.         return $strUser;
  679.     }//function getUserName()
  680.  
  681.  
  682.  
  683.     /**
  684.     *   Returns the windows directory (if any).
  685.     *   NULL is returned if the system is not Windows.
  686.     *
  687.     *   @return string  The windows directory, NULL if not on windows
  688.     *   @access public
  689.     */
  690.     function getWindows()
  691.     {
  692.         if ($this->sys != SYS_WINDOWS) {
  693.             return null;
  694.         }
  695.  
  696.         $strWindows = null;
  697.         $arEnv = $_SERVER + $_ENV;
  698.         if (isset($arEnv['SystemRoot'])) {
  699.             $strWindows = $arEnv['SystemRoot'];
  700.         } else if (isset($arEnv['windir'])) {
  701.             $strWindows = $arEnv['windir'];
  702.         } else {
  703.             $strWindows = System_Folders::tryPaths($this->arWindowsDirs);
  704.         }//no env variable
  705.  
  706.         return $this->addTrailingSlash($this->fixWindowsPath($strWindows));
  707.    }//function getWindows()
  708.  
  709. }//class System_Folders
  710. ?>
  711.