Classes

File io/filesystem/FileSystemElement.class.php

File io/filesystem/FileSystemElement.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 
<?php
/**
 * This class represents any file system element.
 * @package     IO
 * @subpackage  Filesystem
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Nicolas Duhamel <akhenathon2@gmail.com>
 * @version     PHPBoost 5.2 - last update: 2018 11 07
 * @since       PHPBoost 2.0 - 2008 07 06
 * @contributor Loic ROUCHON <horn@phpboost.com>
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
 * @contributor ph-7 <me@ph7.me>
*/

abstract class FileSystemElement
{
    /**
     * @var string Path of the file system element
     */
    protected $path;

    /**
     * Builds a FileSystemElement object from the path of the element.
     * @param string $path Path of the element
     */
    protected function __construct($path)
    {
        $this->path = Path::uniformize_path($path);
    }

    /**
     * Allows you to know if the file system element exists.
     * @return bool True if the file exists, else, false.
     */
    public function exists()
    {
        return file_exists($this->path);
    }

    /**
     * Returns the element full path.
     * @return string The element full path.
     */
    public function get_path()
    {
        return $this->path;
    }

    /**
     * Returns the element path from the phpboost root.
     * @return string The element from the phpboost root.
     */
    public function get_path_from_root()
    {
        $path_from_root = Path::get_path_from_root($this->path);
        if (empty($path_from_root))
        {
            return $this->path;
        }
        return $path_from_root;
    }

    /**
     * Returns the element name.
     * @return string The element name.
     */
    public function get_name()
    {
        if (mb_strpos($this->path, '/') !== false)
        {
            $parts = explode('/', trim($this->path, '/'));
            return $parts[count($parts) - 1];
        }
        return $this->path;
    }

    /**
     * Returns true if the file or the folder is writable.
     * @param bool $force_chmod If true, then, chmod will be forced to 777 if not writable.
     * @return true if the file or the folder is writable.
     */
    public function is_writable($force_chmod = false)
    {
        if (!$this->exists())
        {
            return false;
        }
        else if (@is_writable($this->path))
        {
            return true;
        }
        else if ($force_chmod)
        {
            return $folder->change_chmod(0777) && @is_writable($this->path);
        }
        return false;
    }

    /**
     * Changes the chmod of the element.
     * @param int $chmod The new chmod of the file. Put a 0 at the begening of the number to indicate to the PHP parser that it's an octal value.
     * @return true if the chmod has been successfully changed.
     */
    public function change_chmod($chmod)
    {
        if (!empty($this->path))
        {
            return chmod($this->path, $chmod);
        }
        return false;
    }

    /**
     * @abstract
     * Deletes the element
     */
    public abstract function delete();
}
?>