View source
<?php
namespace Drupal\hacked;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Component\Utility\Unicode;
use Exception;
class hackedProjectWebFilesDownloader extends hackedProjectWebDownloader {
function download_link() {
if (!empty($this->project->project_info['releases'][$this->project->existing_version])) {
$this_release = $this->project->project_info['releases'][$this->project->existing_version];
return $this_release['download_link'];
}
}
function download() {
$dir = $this
->get_destination();
if (!($release_url = $this
->download_link())) {
return FALSE;
}
if (file_exists($dir) && count(hacked_file_scan_directory($dir, '/.*/', [
'.',
'..',
'CVS',
'.svn',
'.git',
]))) {
return $dir;
}
if (!\Drupal::service('file_system')
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
$message = $this
->t('Failed to create temp directory: %dir', [
'%dir' => $dir,
]);
\Drupal::logger('hacked')
->error($message
->render());
return FALSE;
}
if (!($local_file = $this
->file_get($release_url))) {
$message = $this
->t('Could not download the project: @name from URL: @url', [
'@name' => $this->project
->title(),
'@url' => $release_url,
]);
\Drupal::logger('hacked')
->error($message
->render());
return FALSE;
}
try {
$this
->archive_extract($local_file, $dir);
} catch (Exception $e) {
$message = $this
->t('Could not extract the project: @name. Error was: !error', [
'@name' => $this->project
->title(),
'!error' => $e
->getMessage(),
]);
\Drupal::logger('hacked')
->error($message
->render());
return FALSE;
}
return TRUE;
}
function file_get($url) {
$parsed_url = parse_url($url);
$remote_schemes = [
'http',
'https',
'ftp',
'ftps',
'smb',
'nfs',
];
if (!in_array($parsed_url['scheme'], $remote_schemes)) {
return \Drupal::service('file_system')
->realpath($url);
}
$cache_directory = 'temporary://hacked-cache';
$local = $cache_directory . '/' . basename($parsed_url['path']);
if (!file_exists($cache_directory)) {
mkdir($cache_directory);
}
return system_retrieve_file($url, $local, FALSE, FileSystemInterface::EXISTS_REPLACE);
}
function archive_extract($file, $directory) {
$archiver = \Drupal::service('plugin.manager.archiver')
->getInstance([
'filepath' => $file,
]);
if (!$archiver) {
throw new Exception(t('Cannot extract %file, not a valid archive.', [
'%file' => $file,
]));
}
$files = $archiver
->listContents();
$project = mb_substr($files[0], 0, -1);
$extract_location = $directory . '/' . $project;
if (file_exists($extract_location)) {
\Drupal::service('file_system')
->deleteRecursive($extract_location);
}
$archiver
->extract($directory);
return $archiver;
}
}