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