Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
FileUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class FileUtil
41  {
51  public static function getFilesFromDir($dir, $basePath, $beginAliasPath, $includeTests = false)
52  {
53  $files = array();
54  if ($handle = opendir($dir))
55  {
56  while (false !== ($file = readdir($handle)))
57  {
58  $includeFile = false;
59  if ($file != 'tests')
60  {
61  $includeFile = true;
62  }
63  elseif ($file == 'tests')
64  {
65  if ($includeTests)
66  {
67  $includeFile = true;
68  }
69  else
70  {
71  $includeFile = false;
72  }
73  }
74  if ($file != "." && $file != ".." && $includeFile)
75  {
76  if (is_dir($dir . DIRECTORY_SEPARATOR . $file))
77  {
78  $dir2 = $dir . DIRECTORY_SEPARATOR . $file;
79  $files[] = self::getFilesFromDir($dir2, $basePath, $beginAliasPath, $includeTests);
80  }
81  elseif (substr(strrchr($file, '.'), 1) == 'php')
82  {
83  $tmp = $dir . DIRECTORY_SEPARATOR . $file;
84  $tmp = str_replace($basePath, $beginAliasPath, $tmp);
85  $tmp = str_replace(DIRECTORY_SEPARATOR, '.', $tmp);
86  $files[] = substr($tmp, 0, -4);
87  }
88  }
89  }
90  closedir($handle);
91  }
92  return ArrayUtil::flatten($files);
93  }
94 
99  public static function getNonWriteableFilesOrFolders($directory, &$nonWritableItems = array())
100  {
101  $isWritable = true;
102  $handle = opendir($directory);
103  while (($item = readdir($handle)) !== false)
104  {
105  if ($item != '.' && $item != '..')
106  {
107  $path = $directory . '/' . $item;
108  if (is_dir($path))
109  {
110  // Check if folder itself is writeable, and if all subfolders and files are writeable.
111  $nonWritableItems = self::getNonWriteableFilesOrFolders($path, $nonWritableItems);
112  $isWritable = is_writeable($path) && empty($nonWritableItems);
113  }
114  else
115  {
116  $isWritable = is_writeable($path);
117  }
118  if (!$isWritable)
119  {
120  $nonWritableItems[] = $path;
121  }
122  }
123  }
124  closedir($handle);
125  return $nonWritableItems;
126  }
127 
133  public static function copyRecursive($source, $target)
134  {
135  if (is_dir($source))
136  {
137  @mkdir($target);
138  $currentWorkingDirectory = dir($source);
139  while (false !== ($filename = $currentWorkingDirectory->read()))
140  {
141  if ($filename == '.' || $filename == '..')
142  {
143  continue;
144  }
145  $fullPath = $source . '/' . $filename;
146  if (is_dir($fullPath))
147  {
148  self::copyRecursive($fullPath, $target . '/' . $filename);
149  continue;
150  }
151  copy($fullPath, $target . '/' . $filename);
152  }
153  $currentWorkingDirectory->close();
154  }
155  elseif (is_file($source))
156  {
157  copy($source, $target );
158  }
159  }
160 
167  public static function deleteDirectoryRecursive($directory, $removeDirectoryItself = true, $filesOrFoldersToSkip = array())
168  {
169  assert(is_dir($directory)); // Not Coding Standard
170  assert(($removeDirectoryItself && empty($skip)) || !$removeDirectoryItself); // Not Coding Standard
171  $entries = scandir($directory);
172  foreach ($entries as $entry)
173  {
174  if ($entry != "." && $entry != "..")
175  {
176  if (!empty($filesOrFoldersToSkip))
177  {
178  $skipFileOrFolder = false;
179  foreach ($filesOrFoldersToSkip as $fileOrFoldersToSkip)
180  {
181  if (trim($fileOrFoldersToSkip, DIRECTORY_SEPARATOR) == trim($entry, DIRECTORY_SEPARATOR))
182  {
183  $skipFileOrFolder = true;
184  }
185  }
186  if ($skipFileOrFolder)
187  {
188  continue;
189  }
190  }
191  $entry = "$directory/$entry";
192  if (is_file($entry) || is_link($entry))
193  {
194  unlink($entry);
195  }
196  else
197  {
198  self::deleteDirectoryRecursive($entry, true);
199  }
200  }
201  }
202  if ($removeDirectoryItself)
203  {
204  rmdir($directory);
205  }
206  }
207  }
208 ?>
static flatten($array, $preserveKeys=false)
Definition: ArrayUtil.php:120
static copyRecursive($source, $target)
Definition: FileUtil.php:133
static getNonWriteableFilesOrFolders($directory, &$nonWritableItems=array())
Definition: FileUtil.php:99
static getFilesFromDir($dir, $basePath, $beginAliasPath, $includeTests=false)
Definition: FileUtil.php:51
static deleteDirectoryRecursive($directory, $removeDirectoryItself=true, $filesOrFoldersToSkip=array())
Definition: FileUtil.php:167
Generated on Mon Jul 6 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.