2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* A collection of static methods for manipulating the filesystem.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage filesystem
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Filesystem extends Object {
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var integer Integer
|
|
|
|
*/
|
|
|
|
private static $file_create_mask = 02775;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var integer Integer
|
|
|
|
*/
|
|
|
|
private static $folder_create_mask = 02775;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-05-11 05:07:12 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2008-09-11 00:01:09 +02:00
|
|
|
protected static $cache_folderModTime;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-05-11 05:07:12 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* Array of file / folder regex expressions to exclude from the
|
2013-05-11 05:07:12 +02:00
|
|
|
* {@link Filesystem::sync()}
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $sync_blacklisted_patterns = array(
|
|
|
|
"/^\./",
|
|
|
|
"/^_combinedfiles$/i",
|
|
|
|
"/^_resampled$/i",
|
|
|
|
"/^web.config/i",
|
|
|
|
"/^Thumbs(.)/"
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Create a folder on the filesystem, recursively.
|
|
|
|
* Uses {@link Filesystem::$folder_create_mask} to set filesystem permissions.
|
|
|
|
* Use {@link Folder::findOrMake()} to create a {@link Folder} database
|
|
|
|
* record automatically.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $folder Absolute folder path
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function makeFolder($folder) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!file_exists($base = dirname($folder))) self::makeFolder($base);
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!file_exists($folder)) mkdir($folder, Config::inst()->get('Filesystem', 'folder_create_mask'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Remove a directory and all subdirectories and files.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $folder Absolute folder path
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param Boolean $contentsOnly If this is true then the contents of the folder will be removed but not the
|
|
|
|
* folder itself
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function removeFolder($folder, $contentsOnly = false) {
|
2010-10-19 07:06:25 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// remove a file encountered by a recursive call.
|
2010-10-19 07:06:25 +02:00
|
|
|
if(is_file($folder) || is_link($folder)) {
|
|
|
|
unlink($folder);
|
|
|
|
} else {
|
|
|
|
$dir = opendir($folder);
|
|
|
|
while($file = readdir($dir)) {
|
|
|
|
if(($file == '.' || $file == '..')) continue;
|
|
|
|
else {
|
|
|
|
self::removeFolder($folder . '/' . $file);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
closedir($dir);
|
2008-08-12 04:51:33 +02:00
|
|
|
if(!$contentsOnly) rmdir($folder);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2015-08-17 18:31:12 +02:00
|
|
|
/**
|
|
|
|
* Remove a directory, but only if it is empty.
|
|
|
|
*
|
|
|
|
* @param string $folder Absolute folder path
|
|
|
|
* @param boolean $recursive Remove contained empty folders before attempting to remove this one
|
|
|
|
* @return boolean True on success, false on failure.
|
|
|
|
*/
|
|
|
|
public static function remove_folder_if_empty($folder, $recursive = true) {
|
|
|
|
if (!is_readable($folder)) return false;
|
|
|
|
$handle = opendir($folder);
|
|
|
|
while (false !== ($entry = readdir($handle))) {
|
|
|
|
if ($entry != "." && $entry != "..") {
|
|
|
|
// if an empty folder is detected, remove that one first and move on
|
|
|
|
if($recursive && is_dir($entry) && self::remove_folder_if_empty($entry)) continue;
|
|
|
|
// if a file was encountered, or a subdirectory was not empty, return false.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
// if we are still here, the folder is empty.
|
2015-08-17 18:31:12 +02:00
|
|
|
rmdir($folder);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* Cleanup function to reset all the Filename fields. Visit File/fixfiles to call.
|
|
|
|
*/
|
|
|
|
public function fixfiles() {
|
2009-09-10 04:00:42 +02:00
|
|
|
if(!Permission::check('ADMIN')) return Security::permissionFailure($this);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
$files = DataObject::get("File");
|
|
|
|
foreach($files as $file) {
|
2010-10-15 05:20:47 +02:00
|
|
|
$file->updateFilesystem();
|
2008-04-06 10:20:13 +02:00
|
|
|
echo "<li>", $file->Filename;
|
|
|
|
$file->write();
|
|
|
|
}
|
|
|
|
echo "<p>Done!";
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
2007-07-19 12:40:28 +02:00
|
|
|
* Return the most recent modification time of anything in the folder.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param $folder The folder, relative to the site root
|
|
|
|
* @param $extensionList An option array of file extensions to limit the search to
|
2010-10-15 05:12:39 +02:00
|
|
|
* @return String Same as filemtime() format.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function folderModTime($folder, $extensionList = null, $recursiveCall = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
//$cacheID = $folder . ',' . implode(',', $extensionList);
|
|
|
|
//if(!$recursiveCall && self::$cache_folderModTime[$cacheID]) return self::$cache_folderModTime[$cacheID];
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$modTime = 0;
|
|
|
|
if(!Filesystem::isAbsolute($folder)) $folder = Director::baseFolder() . '/' . $folder;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = scandir($folder);
|
|
|
|
foreach($items as $item) {
|
|
|
|
if($item[0] != '.') {
|
|
|
|
// Recurse into folders
|
|
|
|
if(is_dir("$folder/$item")) {
|
|
|
|
$modTime = max($modTime, self::folderModTime("$folder/$item", $extensionList, true));
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Check files
|
|
|
|
} else {
|
|
|
|
if($extensionList) $extension = strtolower(substr($item,strrpos($item,'.')+1));
|
|
|
|
if(!$extensionList || in_array($extension, $extensionList)) {
|
|
|
|
$modTime = max($modTime, filemtime("$folder/$item"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if(!$recursiveCall) self::$cache_folderModTime[$cacheID] = $modTime;
|
|
|
|
return $modTime;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given filename is an absolute file reference.
|
2010-10-15 05:12:39 +02:00
|
|
|
* Works on Linux and Windows.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $filename Absolute or relative filename, with or without path.
|
|
|
|
* @return Boolean
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function isAbsolute($filename) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($_ENV['OS'] == "Windows_NT" || $_SERVER['WINDIR']) return $filename[1] == ':' && $filename[2] == '/';
|
|
|
|
else return $filename[0] == '/';
|
|
|
|
}
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
/**
|
|
|
|
* This function ensures the file table is correct with the files in the assets folder.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-10-21 04:33:55 +02:00
|
|
|
* If a Folder record ID is given, all of that folder's children will be synchronised.
|
|
|
|
* If the given Folder ID isn't found, or not specified at all, then everything will
|
|
|
|
* be synchronised from the root folder (singleton Folder).
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* See {@link File->updateFilesystem()} to sync properties of a single database record
|
2010-10-15 05:12:39 +02:00
|
|
|
* back to the equivalent filesystem record.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2009-10-21 04:33:55 +02:00
|
|
|
* @param int $folderID Folder ID to sync along with all it's children
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param Boolean $syncLinkTracking Determines if the link tracking data should also
|
2011-09-27 14:02:35 +02:00
|
|
|
* be updated via {@link SiteTree->syncLinkTracking()}. Setting this to FALSE
|
|
|
|
* means that broken links inside page content are not noticed, at least until the next
|
|
|
|
* call to {@link SiteTree->write()} on this page.
|
|
|
|
* @return string Localized status message
|
2008-04-06 10:20:13 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function sync($folderID = null, $syncLinkTracking = true) {
|
2009-10-21 04:33:55 +02:00
|
|
|
$folder = DataObject::get_by_id('Folder', (int) $folderID);
|
|
|
|
if(!($folder && $folder->exists())) $folder = singleton('Folder');
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-21 04:33:55 +02:00
|
|
|
$results = $folder->syncChildren();
|
2008-04-06 10:20:13 +02:00
|
|
|
$finished = false;
|
|
|
|
while(!$finished) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$orphans = DB::query('SELECT "ChildFile"."ID" FROM "File" AS "ChildFile"
|
|
|
|
LEFT JOIN "File" AS "ParentFile" ON "ChildFile"."ParentID" = "ParentFile"."ID"
|
|
|
|
WHERE "ParentFile"."ID" IS NULL AND "ChildFile"."ParentID" > 0');
|
2008-04-06 10:20:13 +02:00
|
|
|
$finished = true;
|
|
|
|
if($orphans) foreach($orphans as $orphan) {
|
|
|
|
$finished = false;
|
|
|
|
// Delete the database record but leave the filesystem alone
|
|
|
|
$file = DataObject::get_by_id("File", $orphan['ID']);
|
|
|
|
$file->deleteDatabaseOnly();
|
2008-05-24 07:23:24 +02:00
|
|
|
unset($file);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-12 03:49:37 +02:00
|
|
|
|
|
|
|
// Update the image tracking of all pages
|
2011-09-27 14:02:35 +02:00
|
|
|
if($syncLinkTracking) {
|
|
|
|
if(class_exists('SiteTree')) {
|
2013-05-13 08:03:20 +02:00
|
|
|
|
|
|
|
// if subsites exist, go through each subsite and sync each subsite's pages.
|
|
|
|
// disabling the filter doesn't work reliably, because writing pages that share
|
|
|
|
// the same URLSegment between subsites will break, e.g. "home" between two
|
|
|
|
// sites will modify one of them to "home-2", thinking it's a duplicate. The
|
|
|
|
// check before a write is done in SiteTree::validURLSegment()
|
|
|
|
if(class_exists('Subsite')) {
|
|
|
|
// loop through each subsite ID, changing the subsite, then query it's pages
|
|
|
|
foreach(Subsite::get()->getIDList() as $id) {
|
|
|
|
Subsite::changeSubsite($id);
|
|
|
|
foreach(SiteTree::get() as $page) {
|
|
|
|
// syncLinkTracking is called by SiteTree::onBeforeWrite().
|
|
|
|
// Call it without affecting the page version, as this is an internal change.
|
|
|
|
$page->writeWithoutVersion();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// change back to the main site so the foreach below works
|
|
|
|
Subsite::changeSubsite(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(SiteTree::get() as $page) {
|
2011-09-27 14:02:35 +02:00
|
|
|
// syncLinkTracking is called by SiteTree::onBeforeWrite().
|
|
|
|
// Call it without affecting the page version, as this is an internal change.
|
|
|
|
$page->writeWithoutVersion();
|
|
|
|
}
|
2011-03-23 04:32:24 +01:00
|
|
|
}
|
2010-04-12 03:49:37 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-21 04:29:20 +02:00
|
|
|
return _t(
|
|
|
|
'Filesystem.SYNCRESULTS',
|
2013-06-21 00:32:08 +02:00
|
|
|
'Sync complete: {createdcount} items created, {deletedcount} items deleted',
|
2012-05-20 10:49:12 +02:00
|
|
|
array('createdcount' => (int)$results['added'], 'deletedcount' => (int)$results['deleted'])
|
2009-10-21 04:29:20 +02:00
|
|
|
);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|