Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/filesystem/path.php

Documentation is available at path.php

  1. <?php
  2. /**
  3.  * @version        $Id: path.php 6559 2007-02-10 18:22:43Z friesengeist $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    FileSystem
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. /** boolean True if a Windows based host */
  16. define('JPATH_ISWIN', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'));
  17. /** boolean True if a Mac based host */
  18. define('JPATH_ISMAC', (strtoupper(substr(PHP_OS, 0, 3)) === 'MAC'));
  19.  
  20. if (!defined('DS')) {
  21.     /** string Shortcut for the DIRECTORY_SEPARATOR define */
  22.     define('DS', DIRECTORY_SEPARATOR);
  23. }
  24.  
  25. if (!defined('JPATH_ROOT')) {
  26.     /** string The root directory of the file system in native format */
  27.     define('JPATH_ROOT', JPath::clean(JPATH_SITE));
  28. }
  29.  
  30. /**
  31.  * A Path handling class
  32.  *
  33.  * @static
  34.  * @package     Joomla.Framework
  35.  * @subpackage    FileSystem
  36.  * @since        1.5
  37.  */
  38. class JPath
  39. {
  40.     /**
  41.      * Checks if a path's permissions can be changed
  42.      *
  43.      * @param    string    $path    Path to check
  44.      * @return    boolean    True if path can have mode changed
  45.      * @since    1.5
  46.      */
  47.     function canChmod($path)
  48.     {
  49.         $perms = fileperms($path);
  50.         if ($perms !== false)
  51.         {
  52.             if (@ chmod($path, $perms ^ 0001))
  53.             {
  54.                 @chmod($path, $perms);
  55.                 return true;
  56.             }
  57.         }
  58.         return false;
  59.     }
  60.  
  61.     /**
  62.      * Chmods files and directories recursivly to given permissions
  63.      *
  64.      * @param    string    $path        Root path to begin changing mode [without trailing slash]
  65.      * @param    string    $filemode    Octal representation of the value to change file mode to [null = no change]
  66.      * @param    string    $foldermode    Octal representation of the value to change folder mode to [null = no change]
  67.      * @return    boolean    True if successful [one fail means the whole operation failed]
  68.      * @since    1.5
  69.      */
  70.     function setPermissions($path, $filemode = '0644', $foldermode = '0755') {
  71.  
  72.         // Initialize return value
  73.         $ret = true;
  74.  
  75.         if (is_dir($path))
  76.         {
  77.             $dh = opendir($path);
  78.             while ($file = readdir($dh))
  79.             {
  80.                 if ($file != '.' && $file != '..') {
  81.                     $fullpath = $path.'/'.$file;
  82.                     if (is_dir($fullpath)) {
  83.                         if (!JPath::setPermissions($fullpath, $filemode, $foldermode)) {
  84.                             $ret = false;
  85.                         }
  86.                     } else {
  87.                         if (isset ($filemode)) {
  88.                             if (!@ chmod($fullpath, octdec($filemode))) {
  89.                                 $ret = false;
  90.                             }
  91.                         }
  92.                     } // if
  93.                 } // if
  94.             } // while
  95.             closedir($dh);
  96.             if (isset ($foldermode)) {
  97.                 if (!@ chmod($path, octdec($foldermode))) {
  98.                     $ret = false;
  99.                 }
  100.             }
  101.         }
  102.         else
  103.         {
  104.             if (isset ($filemode)) {
  105.                 $ret = @ chmod($path, octdec($filemode));
  106.             }
  107.         } // if
  108.         return $ret;
  109.     }
  110.  
  111.     /**
  112.      * Get the permissions of the file/folder at a give path
  113.      *
  114.      * @param    string    $path    The path of a file/folder
  115.      * @return    string    Filesystem permissions
  116.      * @since    1.5
  117.      */
  118.     function getPermissions($path)
  119.     {
  120.         $path = JPath::clean($path);
  121.         $mode = @ decoct(@ fileperms($path) & 0777);
  122.  
  123.         if (strlen($mode) < 3) {
  124.             return '---------';
  125.         }
  126.         $parsed_mode = '';
  127.         for ($i = 0; $i < 3; $i ++)
  128.         {
  129.             // read
  130.             $parsed_mode .= ($mode { $i } & 04) ? "r" : "-";
  131.             // write
  132.             $parsed_mode .= ($mode { $i } & 02) ? "w" : "-";
  133.             // execute
  134.             $parsed_mode .= ($mode { $i } & 01) ? "x" : "-";
  135.         }
  136.         return $parsed_mode;
  137.     }
  138.  
  139.     /**
  140.      * Checks for snooping outside of the file system root
  141.      *
  142.      * @param    string    $path    A file system path to check
  143.      * @return    string    A cleaned version of the path
  144.      * @since    1.5
  145.      */
  146.     function check($path)
  147.     {
  148.         if (strpos($path, '..') !== false) {
  149.             JError::raiseError( 20, 'JPath::check Use of relative paths not permitted'); // don't translate
  150.             die();
  151.         }
  152.         $path = JPath::clean($path);
  153.         if (strpos($path, JPath::clean(JPATH_ROOT)) !== 0) {
  154.             JError::raiseError( 20, 'JPath::check Snooping out of bounds @ '.$path); // don't translate
  155.             die();
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Function to strip additional / or \ in a path name
  161.      *
  162.      * @static
  163.      * @param    string    $path    The path to clean
  164.      * @param    string    $ds        Directory separator (optional)
  165.      * @return    string    The cleaned path
  166.      * @since    1.5
  167.      */
  168.     function clean($path, $ds=DS)
  169.     {
  170.         $path = trim($path);
  171.  
  172.         if (empty($path)) {
  173.             $path = JPATH_ROOT;
  174.         } else {
  175.             // Remove double slashes and backslahses and convert all slashes and backslashes to DS
  176.             $path = preg_replace('#[/\\\\]+#', $ds, $path);
  177.         }
  178.  
  179.         return $path;
  180.     }
  181.  
  182.     /**
  183.      * Method to determine if script owns the path
  184.      *
  185.      * @static
  186.      * @param    string    $path    Path to check ownership
  187.      * @return    boolean    True if the php script owns the path passed
  188.      * @since    1.5
  189.      */
  190.     function isOwner($path)
  191.     {
  192.         return (posix_getuid() == fileowner($path));
  193.     }
  194.  
  195.     /**
  196.      * Searches the directory paths for a given file.
  197.      *
  198.      * @access    protected
  199.      * @param    array    $path    An array of paths to search in
  200.      * @param    string    $file    The file name to look for.
  201.      * @return    mixed    The full path and file name for the target file, or boolean false if the file is not found in any of the paths.
  202.      * @since    1.5
  203.      */
  204.     function find($paths, $file)
  205.     {
  206.         // start looping through the path set
  207.         foreach ($paths as $path)
  208.         {
  209.             // get the path to the file
  210.             $fullname = $path.DS.$file;
  211.  
  212.             // is the path based on a stream?
  213.             if (strpos($path, '://') === false)
  214.             {
  215.                 // not a stream, so do a realpath() to avoid directory
  216.                 // traversal attempts on the local file system.
  217.                 $path = realpath($path); // needed for substr() later
  218.                 $fullname = realpath($fullname);
  219.             }
  220.  
  221.             // the substr() check added to make sure that the realpath()
  222.             // results in a directory registered so that
  223.             // non-registered directores are not accessible via directory
  224.             // traversal attempts.
  225.             if (file_exists($fullname) && substr($fullname, 0, strlen($path)) == $path) {
  226.                 return $fullname;
  227.             }
  228.         }
  229.  
  230.         // could not find the file in the set of paths
  231.         return false;
  232.     }
  233. }
  234. ?>

Documentation generated on Mon, 05 Mar 2007 21:13:51 +0000 by phpDocumentor 1.3.1