View source
<?php
define('HACKED_OP_CHECK_SELECTED', 'check');
define('HACKED_OP_RESTORE_SELECTED', 'restore');
define('HACKED_OP_CHECK_ALL', 'check_all');
define('HACKED_OP_RESTORE_ALL', 'restore_all');
function _hacked_get_temp_dir($subdir = '') {
$hash = substr(drupal_hash_base64(variable_get('site_name')), 0, 8);
$dirname = file_directory_temp() . '/hacked-' . $hash . '/' . $subdir;
if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && !mkdir($dirname, 0777, TRUE)) {
watchdog('hacked', 'Failed to create temp directory: %dirname', [
'%dirname' => $dirname,
], WATCHDOG_ERROR);
return FALSE;
}
return $dirname;
}
function _hacked_remove_directory($dir) {
if (!is_dir($dir)) {
watchdog('hacked', $dir . ' must be a directory');
return FALSE;
}
if (substr($dir, strlen($dir) - 1, 1) != '/') {
$dir .= '/';
}
$files = glob($dir . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
if (!_hacked_remove_directory($file)) {
watchdog('hacked', 'What is wrong');
return FALSE;
}
}
else {
unlink($file);
}
}
rmdir($dir);
return TRUE;
}
function _hacked_copy_directory($src, $dst) {
$dir = opendir($src);
@mkdir($dst);
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
if (is_dir($src . '/' . $file)) {
_hacked_copy_directory($src . '/' . $file, $dst . '/' . $file);
}
else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
function _hacked_get_projects() {
module_load_include('inc', 'update', 'update.report');
if ($available = update_get_available(TRUE)) {
module_load_include('inc', 'update', 'update.compare');
return update_calculate_project_data($available);
}
return FALSE;
}
function _hacked_download_archive($project, $version) {
$filename = $project . '-' . $version . '.zip';
$url = 'https://ftp.drupal.org/files/projects/' . $filename;
$tempdir = _hacked_get_temp_dir();
$filepath = $tempdir . '/' . $filename;
$is_downloaded = file_put_contents($filepath, fopen($url, 'r'));
return $is_downloaded ? $filepath : FALSE;
}
function _hacked_extract_archive($filepath) {
$archiver = archiver_get_archiver($filepath);
if (!$archiver) {
return FALSE;
}
$archiver
->extract(_hacked_get_temp_dir());
return $archiver;
}
function _hacked_check_project($project) {
$filepath = _hacked_download_archive($project['name'], $project['existing_version']);
if (!$filepath) {
watchdog('hacked', 'Can not download file' . $filepath);
return FALSE;
}
$extract = _hacked_extract_archive($filepath);
if (!$extract) {
watchdog('hacked', 'Can extract archive' . $filepath);
return FALSE;
}
}
function _hacked_restore_project($project) {
if ($project['name'] == 'drupal') {
watchdog('hacked', 'Restore core is unsupported now');
return TRUE;
}
_hacked_check_project($project);
$project_dir = drupal_get_path($project['project_type'], $project['name']);
if ($project_dir) {
$tmp_project_dir = _hacked_get_temp_dir() . '/' . $project['name'];
if (!_hacked_remove_directory($project_dir)) {
watchdog('hacked', 'Can not remove project directory');
return FALSE;
}
_hacked_copy_directory($tmp_project_dir, $project_dir);
return TRUE;
}
else {
watchdog('hacked', 'Can not get project directory: ' . $project['name']);
return FALSE;
}
}
function _hacked_report_batch($operation, $projects = NULL) {
$batch_operations = [];
switch ($operation) {
case HACKED_OP_CHECK_SELECTED:
$selected_projects = array_intersect_key(_hacked_get_projects(), $projects);
foreach ($selected_projects as $project) {
$batch_operations[] = [
'_hacked_check_project',
[
$project,
],
];
}
break;
case HACKED_OP_RESTORE_SELECTED:
$selected_projects = array_intersect_key(_hacked_get_projects(), $projects);
foreach ($selected_projects as $project) {
$batch_operations[] = [
'_hacked_restore_project',
[
$project,
],
];
}
break;
case HACKED_OP_CHECK_ALL:
foreach (_hacked_get_projects() as $project) {
$batch_operations[] = [
'_hacked_check_project',
[
$project,
],
];
}
break;
case HACKED_OP_RESTORE_ALL:
foreach (_hacked_get_projects() as $project) {
$batch_operations[] = [
'_hacked_restore_project',
[
$project,
],
];
}
break;
}
$batch = [
'operations' => $batch_operations,
'title' => t('Building report'),
];
batch_set($batch);
}