1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149:
<?php
class FileSystemHelper
{
public static function remove_folder($folder, $protect_folder = false, $first_folder_name = '', $parent_folder = '')
{
if ($protect_folder && empty($first_folder_name))
$first_folder_name = $folder;
if (empty($parent_folder))
$parent_folder = $folder;
if (!preg_match( "/^.*\/$/", $folder))
$folder .= '/';
$handle = @opendir($folder);
if ($handle != false)
{
while ($item = readdir($handle))
{
$protect_folder_delete = $protect_folder ? !preg_match('/' . $first_folder . '/', $item) : true;
if ($item != "." && $item != ".." && $protect_folder_delete)
{
$full_path = $folder . $item;
if (is_dir($full_path))
self::remove_folder($full_path, $protect_folder, $first_folder_name, $parent_folder);
else
unlink($full_path);
}
}
closedir ($handle);
$protect_folder_delete = $protect_folder ? !preg_match('/' . $first_folder . '/', $folder) : true;
if ($protect_folder_delete)
$result = rmdir($folder);
}
else
$result = false;
return $result;
}
public static function copy_folder($source_path, $destination_path)
{
if (!preg_match( "/^.*\/$/", $source_path))
$source_path .= '/';
if (!preg_match( "/^.*\/$/", $destination_path))
$destination_path .= '/';
if (is_dir($source_path))
{
if ($dh = opendir($source_path))
{
while (($file = readdir($dh)) !== false)
{
if (!is_dir($destination_path))
mkdir($destination_path, 0755);
if (is_dir($source_path . $file) && $file != '..' && $file != '.')
self::copy_folder($source_path . $file . '/', $destination_path . $file . '/');
elseif ($file != '..' && $file != '.')
copy($source_path . $file, $destination_path . $file);
}
closedir ($dh);
}
}
}
public static function download_remote_file($url, $destination_path, $extract_archive = true, $retry = false)
{
if (!preg_match( "/^.*\/$/", $destination_path))
$destination_path .= '/';
$server_configuration = new ServerConfiguration();
if ($server_configuration->has_curl_library() && Url::check_url_validity($url))
{
$file_basename = basename($url);
$file_extension = substr($file_basename, strrpos($file_basename, '.') + 1);
$file_name = PATH_TO_ROOT . '/cache/' . $file_basename;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
file_put_contents($file_name, $content);
$file = new File($file_name);
if ($file->exists())
{
if (File::get_file_checksum($url) != File::get_file_checksum($file_name))
{
if (!$retry)
FileSystemHelper::download_remote_file($url, $destination_path, $extract_archive, true);
else
return false;
}
if ($extract_archive && $file_extension == 'zip')
{
$zip_archive = new ZipArchive();
$zip_archive->open($file_name);
$zip_archive->extractTo($destination_path);
$zip_archive->close();
unlink($file_name);
}
return true;
}
return false;
}
return false;
}
}
?>