Merge pull request #8025 from monishdeb/CRM-18236
[civicrm-core.git] / CRM / Utils / File.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
35 * class to provide simple static functions for file objects
36 */
37class CRM_Utils_File {
38
39 /**
40 * Given a file name, determine if the file contents make it an ascii file
41 *
77855840
TO
42 * @param string $name
43 * Name of file.
6a488035 44 *
ae5ffbb7 45 * @return bool
a6c01b45 46 * true if file is ascii
6a488035 47 */
00be9182 48 public static function isAscii($name) {
6a488035
TO
49 $fd = fopen($name, "r");
50 if (!$fd) {
51 return FALSE;
52 }
53
54 $ascii = TRUE;
55 while (!feof($fd)) {
56 $line = fgets($fd, 8192);
57 if (!CRM_Utils_String::isAscii($line)) {
58 $ascii = FALSE;
59 break;
60 }
61 }
62
63 fclose($fd);
64 return $ascii;
65 }
66
67 /**
68 * Given a file name, determine if the file contents make it an html file
69 *
77855840
TO
70 * @param string $name
71 * Name of file.
6a488035 72 *
ae5ffbb7 73 * @return bool
a6c01b45 74 * true if file is html
6a488035 75 */
00be9182 76 public static function isHtml($name) {
6a488035
TO
77 $fd = fopen($name, "r");
78 if (!$fd) {
79 return FALSE;
80 }
81
82 $html = FALSE;
83 $lineCount = 0;
84 while (!feof($fd) & $lineCount <= 5) {
85 $lineCount++;
86 $line = fgets($fd, 8192);
87 if (!CRM_Utils_String::isHtml($line)) {
88 $html = TRUE;
89 break;
90 }
91 }
92
93 fclose($fd);
94 return $html;
95 }
96
97 /**
100fef9d 98 * Create a directory given a path name, creates parent directories
6a488035
TO
99 * if needed
100 *
77855840
TO
101 * @param string $path
102 * The path name.
103 * @param bool $abort
104 * Should we abort or just return an invalid code.
3daed292
TO
105 * @return bool|NULL
106 * NULL: Folder already exists or was not specified.
107 * TRUE: Creation succeeded.
108 * FALSE: Creation failed.
6a488035 109 */
00be9182 110 public static function createDir($path, $abort = TRUE) {
6a488035 111 if (is_dir($path) || empty($path)) {
3daed292 112 return NULL;
6a488035
TO
113 }
114
115 CRM_Utils_File::createDir(dirname($path), $abort);
116 if (@mkdir($path, 0777) == FALSE) {
117 if ($abort) {
118 $docLink = CRM_Utils_System::docURL2('Moving an Existing Installation to a New Server or Location', NULL, NULL, NULL, NULL, "wiki");
119 echo "Error: Could not create directory: $path.<p>If you have moved an existing CiviCRM installation from one location or server to another there are several steps you will need to follow. They are detailed on this CiviCRM wiki page - {$docLink}. A fix for the specific problem that caused this error message to be displayed is to set the value of the config_backend column in the civicrm_domain table to NULL. However we strongly recommend that you review and follow all the steps in that document.</p>";
120
121 CRM_Utils_System::civiExit();
122 }
123 else {
124 return FALSE;
125 }
126 }
127 return TRUE;
128 }
129
130 /**
100fef9d 131 * Delete a directory given a path name, delete children directories
6a488035
TO
132 * and files if needed
133 *
77855840
TO
134 * @param string $target
135 * The path name.
f4aaa82a
EM
136 * @param bool $rmdir
137 * @param bool $verbose
138 *
139 * @throws Exception
6a488035 140 */
00be9182 141 public static function cleanDir($target, $rmdir = TRUE, $verbose = TRUE) {
6a488035
TO
142 static $exceptions = array('.', '..');
143 if ($target == '' || $target == '/') {
144 throw new Exception("Overly broad deletion");
145 }
146
5e7670b1 147 if ($dh = @opendir($target)) {
148 while (FALSE !== ($sibling = readdir($dh))) {
6a488035
TO
149 if (!in_array($sibling, $exceptions)) {
150 $object = $target . DIRECTORY_SEPARATOR . $sibling;
151
152 if (is_dir($object)) {
153 CRM_Utils_File::cleanDir($object, $rmdir, $verbose);
154 }
155 elseif (is_file($object)) {
156 if (!unlink($object)) {
157 CRM_Core_Session::setStatus(ts('Unable to remove file %1', array(1 => $object)), ts('Warning'), 'error');
e7292422 158 }
6a488035
TO
159 }
160 }
161 }
5e7670b1 162 closedir($dh);
6a488035
TO
163
164 if ($rmdir) {
165 if (rmdir($target)) {
166 if ($verbose) {
450f494d 167 CRM_Core_Session::setStatus(ts('Removed directory %1', array(1 => $target)), '', 'success');
6a488035
TO
168 }
169 return TRUE;
e7292422 170 }
6a488035
TO
171 else {
172 CRM_Core_Session::setStatus(ts('Unable to remove directory %1', array(1 => $target)), ts('Warning'), 'error');
e7292422
TO
173 }
174 }
6a488035
TO
175 }
176 }
177
7f616c07
TO
178 /**
179 * Concatenate several files.
180 *
181 * @param array $files
182 * List of file names.
183 * @param string $delim
184 * An optional delimiter to put between files.
185 * @return string
186 */
187 public static function concat($files, $delim = '') {
188 $buf = '';
189 $first = TRUE;
190 foreach ($files as $file) {
191 if (!$first) {
192 $buf .= $delim;
193 }
194 $buf .= file_get_contents($file);
195 $first = FALSE;
196 }
197 return $buf;
198 }
199
5bc392e6 200 /**
ae5ffbb7
TO
201 * @param string $source
202 * @param string $destination
5bc392e6 203 */
ae5ffbb7 204 public static function copyDir($source, $destination) {
5e7670b1 205 if ($dh = opendir($source)) {
948d11bf 206 @mkdir($destination);
5e7670b1 207 while (FALSE !== ($file = readdir($dh))) {
948d11bf
CB
208 if (($file != '.') && ($file != '..')) {
209 if (is_dir($source . DIRECTORY_SEPARATOR . $file)) {
210 CRM_Utils_File::copyDir($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
211 }
212 else {
213 copy($source . DIRECTORY_SEPARATOR . $file, $destination . DIRECTORY_SEPARATOR . $file);
214 }
6a488035
TO
215 }
216 }
5e7670b1 217 closedir($dh);
6a488035 218 }
6a488035
TO
219 }
220
221 /**
222 * Given a file name, recode it (in place!) to UTF-8
223 *
77855840
TO
224 * @param string $name
225 * Name of file.
6a488035 226 *
ae5ffbb7 227 * @return bool
a6c01b45 228 * whether the file was recoded properly
6a488035 229 */
00be9182 230 public static function toUtf8($name) {
6a488035
TO
231 static $config = NULL;
232 static $legacyEncoding = NULL;
233 if ($config == NULL) {
234 $config = CRM_Core_Config::singleton();
235 $legacyEncoding = $config->legacyEncoding;
236 }
237
238 if (!function_exists('iconv')) {
239
240 return FALSE;
241
242 }
243
244 $contents = file_get_contents($name);
245 if ($contents === FALSE) {
246 return FALSE;
247 }
248
249 $contents = iconv($legacyEncoding, 'UTF-8', $contents);
250 if ($contents === FALSE) {
251 return FALSE;
252 }
253
254 $file = fopen($name, 'w');
255 if ($file === FALSE) {
256 return FALSE;
257 }
258
259 $written = fwrite($file, $contents);
260 $closed = fclose($file);
261 if ($written === FALSE or !$closed) {
262 return FALSE;
263 }
264
265 return TRUE;
266 }
267
268 /**
5c8cb77f 269 * Appends a slash to the end of a string if it doesn't already end with one
6a488035 270 *
5c8cb77f
CW
271 * @param string $path
272 * @param string $slash
f4aaa82a 273 *
6a488035 274 * @return string
6a488035 275 */
00be9182 276 public static function addTrailingSlash($path, $slash = NULL) {
5c8cb77f 277 if (!$slash) {
ea3ddccf 278 // FIXME: Defaulting to backslash on windows systems can produce
50bfb460 279 // unexpected results, esp for URL strings which should always use forward-slashes.
5c8cb77f
CW
280 // I think this fn should default to forward-slash instead.
281 $slash = DIRECTORY_SEPARATOR;
6a488035 282 }
5c8cb77f
CW
283 if (!in_array(substr($path, -1, 1), array('/', '\\'))) {
284 $path .= $slash;
6a488035 285 }
5c8cb77f 286 return $path;
6a488035
TO
287 }
288
5bc392e6
EM
289 /**
290 * @param $dsn
100fef9d 291 * @param string $fileName
5bc392e6
EM
292 * @param null $prefix
293 * @param bool $isQueryString
294 * @param bool $dieOnErrors
295 */
00be9182 296 public static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $isQueryString = FALSE, $dieOnErrors = TRUE) {
6a488035
TO
297 require_once 'DB.php';
298
299 $db = DB::connect($dsn);
300 if (PEAR::isError($db)) {
301 die("Cannot open $dsn: " . $db->getMessage());
302 }
303 if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) {
304 $db->query('SET SESSION sql_mode = STRICT_TRANS_TABLES');
305 }
306
307 if (!$isQueryString) {
308 $string = $prefix . file_get_contents($fileName);
309 }
310 else {
311 // use filename as query string
312 $string = $prefix . $fileName;
313 }
314
50bfb460 315 // get rid of comments starting with # and --
6a488035
TO
316
317 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
318 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
319
320 $queries = preg_split('/;\s*$/m', $string);
321 foreach ($queries as $query) {
322 $query = trim($query);
323 if (!empty($query)) {
324 CRM_Core_Error::debug_query($query);
325 $res = &$db->query($query);
326 if (PEAR::isError($res)) {
327 if ($dieOnErrors) {
328 die("Cannot execute $query: " . $res->getMessage());
329 }
330 else {
331 echo "Cannot execute $query: " . $res->getMessage() . "<p>";
332 }
333 }
334 }
335 }
336 }
337
5bc392e6
EM
338 /**
339 * @param $ext
340 *
341 * @return bool
342 */
00be9182 343 public static function isExtensionSafe($ext) {
6a488035
TO
344 static $extensions = NULL;
345 if (!$extensions) {
346 $extensions = CRM_Core_OptionGroup::values('safe_file_extension', TRUE);
347
50bfb460 348 // make extensions to lowercase
6a488035
TO
349 $extensions = array_change_key_case($extensions, CASE_LOWER);
350 // allow html/htm extension ONLY if the user is admin
351 // and/or has access CiviMail
352 if (!(CRM_Core_Permission::check('access CiviMail') ||
353ffa53
TO
353 CRM_Core_Permission::check('administer CiviCRM') ||
354 (CRM_Mailing_Info::workflowEnabled() &&
355 CRM_Core_Permission::check('create mailings')
356 )
357 )
358 ) {
6a488035
TO
359 unset($extensions['html']);
360 unset($extensions['htm']);
361 }
362 }
50bfb460 363 // support lower and uppercase file extensions
6a488035
TO
364 return isset($extensions[strtolower($ext)]) ? TRUE : FALSE;
365 }
366
367 /**
fe482240 368 * Determine whether a given file is listed in the PHP include path.
6a488035 369 *
77855840
TO
370 * @param string $name
371 * Name of file.
6a488035 372 *
ae5ffbb7 373 * @return bool
a6c01b45 374 * whether the file can be include()d or require()d
6a488035 375 */
00be9182 376 public static function isIncludable($name) {
6a488035
TO
377 $x = @fopen($name, 'r', TRUE);
378 if ($x) {
379 fclose($x);
380 return TRUE;
381 }
382 else {
383 return FALSE;
384 }
385 }
386
387 /**
ea3ddccf 388 * Remove the 32 bit md5 we add to the fileName also remove the unknown tag if we added it.
389 *
390 * @param $name
391 *
392 * @return mixed
6a488035 393 */
00be9182 394 public static function cleanFileName($name) {
6a488035
TO
395 // replace the last 33 character before the '.' with null
396 $name = preg_replace('/(_[\w]{32})\./', '.', $name);
397 return $name;
398 }
399
5bc392e6 400 /**
100fef9d 401 * @param string $name
5bc392e6
EM
402 *
403 * @return string
404 */
00be9182 405 public static function makeFileName($name) {
353ffa53
TO
406 $uniqID = md5(uniqid(rand(), TRUE));
407 $info = pathinfo($name);
6a488035
TO
408 $basename = substr($info['basename'],
409 0, -(strlen(CRM_Utils_Array::value('extension', $info)) + (CRM_Utils_Array::value('extension', $info) == '' ? 0 : 1))
410 );
411 if (!self::isExtensionSafe(CRM_Utils_Array::value('extension', $info))) {
412 // munge extension so it cannot have an embbeded dot in it
413 // The maximum length of a filename for most filesystems is 255 chars.
414 // We'll truncate at 240 to give some room for the extension.
415 return CRM_Utils_String::munge("{$basename}_" . CRM_Utils_Array::value('extension', $info) . "_{$uniqID}", '_', 240) . ".unknown";
416 }
417 else {
418 return CRM_Utils_String::munge("{$basename}_{$uniqID}", '_', 240) . "." . CRM_Utils_Array::value('extension', $info);
419 }
420 }
421
5bc392e6
EM
422 /**
423 * @param $path
424 * @param $ext
425 *
426 * @return array
427 */
00be9182 428 public static function getFilesByExtension($path, $ext) {
353ffa53 429 $path = self::addTrailingSlash($path);
6a488035 430 $files = array();
948d11bf 431 if ($dh = opendir($path)) {
948d11bf
CB
432 while (FALSE !== ($elem = readdir($dh))) {
433 if (substr($elem, -(strlen($ext) + 1)) == '.' . $ext) {
434 $files[] .= $path . $elem;
435 }
6a488035 436 }
948d11bf 437 closedir($dh);
6a488035 438 }
6a488035
TO
439 return $files;
440 }
441
442 /**
443 * Restrict access to a given directory (by planting there a restrictive .htaccess file)
444 *
77855840
TO
445 * @param string $dir
446 * The directory to be secured.
f4aaa82a 447 * @param bool $overwrite
6a488035 448 */
00be9182 449 public static function restrictAccess($dir, $overwrite = FALSE) {
6a488035
TO
450 // note: empty value for $dir can play havoc, since that might result in putting '.htaccess' to root dir
451 // of site, causing site to stop functioning.
452 // FIXME: we should do more checks here -
ea3b22b5 453 if (!empty($dir) && is_dir($dir)) {
6a488035
TO
454 $htaccess = <<<HTACCESS
455<Files "*">
456 Order allow,deny
457 Deny from all
458</Files>
459
460HTACCESS;
461 $file = $dir . '.htaccess';
ea3b22b5
TO
462 if ($overwrite || !file_exists($file)) {
463 if (file_put_contents($file, $htaccess) === FALSE) {
464 CRM_Core_Error::movedSiteError($file);
465 }
6a488035
TO
466 }
467 }
468 }
469
af5201d4
TO
470 /**
471 * Restrict remote users from browsing the given directory.
472 *
473 * @param $publicDir
474 */
00be9182 475 public static function restrictBrowsing($publicDir) {
9404eeac
TO
476 if (!is_dir($publicDir) || !is_writable($publicDir)) {
477 return;
478 }
479
af5201d4
TO
480 // base dir
481 $nobrowse = realpath($publicDir) . '/index.html';
482 if (!file_exists($nobrowse)) {
483 @file_put_contents($nobrowse, '');
484 }
485
486 // child dirs
487 $dir = new RecursiveDirectoryIterator($publicDir);
488 foreach ($dir as $name => $object) {
489 if (is_dir($name) && $name != '..') {
490 $nobrowse = realpath($name) . '/index.html';
491 if (!file_exists($nobrowse)) {
492 @file_put_contents($nobrowse, '');
493 }
494 }
495 }
496 }
497
6a488035
TO
498 /**
499 * Create the base file path from which all our internal directories are
500 * offset. This is derived from the template compile directory set
501 */
635f0b86 502 public static function baseFilePath() {
6a488035
TO
503 static $_path = NULL;
504 if (!$_path) {
635f0b86
TO
505 // Note: Don't rely on $config; that creates a dependency loop.
506 if (!defined('CIVICRM_TEMPLATE_COMPILEDIR')) {
507 throw new RuntimeException("Undefined constant: CIVICRM_TEMPLATE_COMPILEDIR");
6a488035 508 }
635f0b86 509 $templateCompileDir = CIVICRM_TEMPLATE_COMPILEDIR;
6a488035
TO
510
511 $path = dirname($templateCompileDir);
512
513 //this fix is to avoid creation of upload dirs inside templates_c directory
514 $checkPath = explode(DIRECTORY_SEPARATOR, $path);
515
516 $cnt = count($checkPath) - 1;
517 if ($checkPath[$cnt] == 'templates_c') {
518 unset($checkPath[$cnt]);
519 $path = implode(DIRECTORY_SEPARATOR, $checkPath);
520 }
521
522 $_path = CRM_Utils_File::addTrailingSlash($path);
523 }
524 return $_path;
525 }
526
9f87b14b
TO
527 /**
528 * Determine if a path is absolute.
529 *
ea3ddccf 530 * @param string $path
531 *
9f87b14b
TO
532 * @return bool
533 * TRUE if absolute. FALSE if relative.
534 */
535 public static function isAbsolute($path) {
536 if (substr($path, 0, 1) === DIRECTORY_SEPARATOR) {
537 return TRUE;
538 }
539 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
540 if (preg_match('!^[a-zA-Z]:[/\\\\]!', $path)) {
541 return TRUE;
542 }
543 }
544 return FALSE;
545 }
546
5bc392e6
EM
547 /**
548 * @param $directory
549 *
550 * @return string
551 */
00be9182 552 public static function relativeDirectory($directory) {
6a488035
TO
553 // Do nothing on windows
554 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
555 return $directory;
556 }
557
558 // check if directory is relative, if so return immediately
9f87b14b 559 if (!self::isAbsolute($directory)) {
6a488035
TO
560 return $directory;
561 }
562
563 // make everything relative from the baseFilePath
564 $basePath = self::baseFilePath();
565 // check if basePath is a substr of $directory, if so
566 // return rest of string
567 if (substr($directory, 0, strlen($basePath)) == $basePath) {
568 return substr($directory, strlen($basePath));
569 }
570
571 // return the original value
572 return $directory;
573 }
574
5bc392e6
EM
575 /**
576 * @param $directory
e3d28c74
TO
577 * @param string|NULL $basePath
578 * The base path when evaluating relative paths. Should include trailing slash.
5bc392e6
EM
579 *
580 * @return string
581 */
e3d28c74 582 public static function absoluteDirectory($directory, $basePath = NULL) {
acc609a7
TO
583 // check if directory is already absolute, if so return immediately
584 // Note: Windows PHP accepts any mix of "/" or "\", so "C:\htdocs" or "C:/htdocs" would be a valid absolute path
585 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && preg_match(';^[a-zA-Z]:[/\\\\];', $directory)) {
6a488035
TO
586 return $directory;
587 }
588
589 // check if directory is already absolute, if so return immediately
590 if (substr($directory, 0, 1) == DIRECTORY_SEPARATOR) {
591 return $directory;
592 }
593
594 // make everything absolute from the baseFilePath
e3d28c74 595 $basePath = ($basePath === NULL) ? self::baseFilePath() : $basePath;
6a488035
TO
596
597 return $basePath . $directory;
598 }
599
600 /**
fe482240 601 * Make a file path relative to some base dir.
6a488035 602 *
f4aaa82a
EM
603 * @param $directory
604 * @param $basePath
605 *
6a488035
TO
606 * @return string
607 */
00be9182 608 public static function relativize($directory, $basePath) {
9f87b14b
TO
609 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
610 $directory = strtr($directory, '\\', '/');
611 $basePath = strtr($basePath, '\\', '/');
612 }
6a488035
TO
613 if (substr($directory, 0, strlen($basePath)) == $basePath) {
614 return substr($directory, strlen($basePath));
0db6c3e1
TO
615 }
616 else {
6a488035
TO
617 return $directory;
618 }
619 }
620
621 /**
fe482240 622 * Create a path to a temporary file which can endure for multiple requests.
6a488035 623 *
50bfb460 624 * @todo Automatic file cleanup using, eg, TTL policy
6a488035 625 *
5a4f6742 626 * @param string $prefix
6a488035
TO
627 *
628 * @return string, path to an openable/writable file
629 * @see tempnam
630 */
00be9182 631 public static function tempnam($prefix = 'tmp-') {
50bfb460
SB
632 // $config = CRM_Core_Config::singleton();
633 // $nonce = md5(uniqid() . $config->dsn . $config->userFrameworkResourceURL);
634 // $fileName = "{$config->configAndLogDir}" . $prefix . $nonce . $suffix;
6a488035
TO
635 $fileName = tempnam(sys_get_temp_dir(), $prefix);
636 return $fileName;
637 }
638
639 /**
fe482240 640 * Create a path to a temporary directory which can endure for multiple requests.
6a488035 641 *
50bfb460 642 * @todo Automatic file cleanup using, eg, TTL policy
6a488035 643 *
5a4f6742 644 * @param string $prefix
6a488035
TO
645 *
646 * @return string, path to an openable/writable directory; ends with '/'
647 * @see tempnam
648 */
00be9182 649 public static function tempdir($prefix = 'tmp-') {
6a488035
TO
650 $fileName = self::tempnam($prefix);
651 unlink($fileName);
652 mkdir($fileName, 0700);
653 return $fileName . '/';
654 }
655
656 /**
d7166b43
TO
657 * Search directory tree for files which match a glob pattern.
658 *
659 * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored.
6a488035 660 *
5a4f6742
CW
661 * @param string $dir
662 * base dir.
663 * @param string $pattern
664 * glob pattern, eg "*.txt".
a2dc0f82
TO
665 * @param bool $relative
666 * TRUE if paths should be made relative to $dir
6a488035
TO
667 * @return array(string)
668 */
a2dc0f82 669 public static function findFiles($dir, $pattern, $relative = FALSE) {
cae27189
TO
670 if (!is_dir($dir)) {
671 return array();
672 }
a2dc0f82 673 $dir = rtrim($dir, '/');
6a488035
TO
674 $todos = array($dir);
675 $result = array();
676 while (!empty($todos)) {
677 $subdir = array_shift($todos);
0b72a00f
TO
678 $matches = glob("$subdir/$pattern");
679 if (is_array($matches)) {
680 foreach ($matches as $match) {
002f1716 681 if (!is_dir($match)) {
a2dc0f82 682 $result[] = $relative ? CRM_Utils_File::relativize($match, "$dir/") : $match;
002f1716 683 }
6a488035
TO
684 }
685 }
948d11bf 686 if ($dh = opendir($subdir)) {
6a488035
TO
687 while (FALSE !== ($entry = readdir($dh))) {
688 $path = $subdir . DIRECTORY_SEPARATOR . $entry;
d7166b43
TO
689 if ($entry{0} == '.') {
690 // ignore
0db6c3e1
TO
691 }
692 elseif (is_dir($path)) {
6a488035
TO
693 $todos[] = $path;
694 }
695 }
696 closedir($dh);
697 }
698 }
699 return $result;
700 }
701
702 /**
703 * Determine if $child is a sub-directory of $parent
704 *
705 * @param string $parent
706 * @param string $child
f4aaa82a
EM
707 * @param bool $checkRealPath
708 *
6a488035
TO
709 * @return bool
710 */
00be9182 711 public static function isChildPath($parent, $child, $checkRealPath = TRUE) {
6a488035
TO
712 if ($checkRealPath) {
713 $parent = realpath($parent);
714 $child = realpath($child);
715 }
716 $parentParts = explode('/', rtrim($parent, '/'));
717 $childParts = explode('/', rtrim($child, '/'));
718 while (($parentPart = array_shift($parentParts)) !== NULL) {
719 $childPart = array_shift($childParts);
720 if ($parentPart != $childPart) {
721 return FALSE;
722 }
723 }
724 if (empty($childParts)) {
725 return FALSE; // same directory
0db6c3e1
TO
726 }
727 else {
6a488035
TO
728 return TRUE;
729 }
730 }
731
732 /**
733 * Move $fromDir to $toDir, replacing/deleting any
734 * pre-existing content.
735 *
77855840
TO
736 * @param string $fromDir
737 * The directory which should be moved.
738 * @param string $toDir
739 * The new location of the directory.
f4aaa82a
EM
740 * @param bool $verbose
741 *
a6c01b45
CW
742 * @return bool
743 * TRUE on success
6a488035 744 */
00be9182 745 public static function replaceDir($fromDir, $toDir, $verbose = FALSE) {
6a488035
TO
746 if (is_dir($toDir)) {
747 if (!self::cleanDir($toDir, TRUE, $verbose)) {
748 return FALSE;
749 }
750 }
751
50bfb460 752 // return rename($fromDir, $toDir); CRM-11987, https://bugs.php.net/bug.php?id=54097
6a488035
TO
753
754 CRM_Utils_File::copyDir($fromDir, $toDir);
755 if (!CRM_Utils_File::cleanDir($fromDir, TRUE, FALSE)) {
e7292422 756 CRM_Core_Session::setStatus(ts('Failed to clean temp dir: %1', array(1 => $fromDir)), '', 'alert');
6a488035
TO
757 return FALSE;
758 }
759 return TRUE;
760 }
96025800 761
6a488035 762}