Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Admin/Tools.php - 99 lines - 2220 bytes - Summary - Text - Print

Description: Remove files and directories.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Remove files and directories.
  26   *
  27   * <code>
  28   * Txp::get('\Textpattern\Admin\Tools')->removeFiles(txpath, 'setup');
  29   * </code>
  30   *
  31   * @since   4.6.0
  32   * @package Admin\Tools
  33   */
  34  
  35  namespace Textpattern\Admin;
  36  
  37  class Tools
  38  {
  39      /**
  40       * Warnings.
  41       *
  42       * @var array
  43       */
  44  
  45      private $warnings;
  46  
  47      /**
  48       * Constructor.
  49       */
  50  
  51      public function __construct()
  52      {
  53          $this->warnings = array();
  54      }
  55  
  56      /**
  57       * Removes files and directories.
  58       *
  59       * @param  string $root  The parent directory
  60       * @param  array  $files Files to remove
  61       * @return bool   The result
  62       */
  63  
  64      public static function removeFiles($root, $files = null)
  65      {
  66          if (!is_dir($root) || !is_writable($root)) {
  67              return false;
  68          } elseif (!isset($files)) {
  69              $files = array_diff(scandir($root), array('.', '..'));
  70          } elseif (!is_array($files)) {
  71              $files = do_list_unique($files);
  72          }
  73  
  74          $result = true;
  75  
  76          foreach ($files as $file) {
  77              $file = $root.DS.$file;
  78  
  79              if (is_file($file)) {
  80                  $result &= unlink($file);
  81              } elseif ($result &= self::removeFiles($file)) {
  82                  $result &= rmdir($file);
  83              }
  84          }
  85  
  86          return $result;
  87      }
  88  
  89      /**
  90       * Outputs warnings.
  91       *
  92       * @return array The warnings
  93       */
  94  
  95      public function getWarnings()
  96      {
  97          return $this->warnings;
  98      }
  99  }

title

Description

title

Description

title

Description

title

title

Body