You are here

hacked.helpers.inc in Hacked! 7.3

Contain list of helpers for the module.

File

hacked.helpers.inc
View source
<?php

/**
 * @file
 * Contain list of helpers for the module.
 */
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');

/**
 * Create and get temp directory.
 */
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;
}

/**
 * Remove directory with files.
 */
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;
}

/**
 * Copy directory with files.
 */
function _hacked_copy_directory($src, $dst) {

  // TODO needs checks if copy fails
  $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);
}

/**
 * Get modules and themes.
 */
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;
}

/**
 * Download archive.
 */
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;
}

/**
 * Extract archive.
 */
function _hacked_extract_archive($filepath) {
  $archiver = archiver_get_archiver($filepath);
  if (!$archiver) {
    return FALSE;
  }
  $archiver
    ->extract(_hacked_get_temp_dir());
  return $archiver;
}

/**
 * Check core, module or theme.
 */
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;
  }

  // TODO compare project with files from archive
}

/**
 * Restore core, module or theme.
 */
function _hacked_restore_project($project) {

  // TODO: restore core is unsupported now
  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;
  }
}

/**
 * Compute the report data for hacked.
 */
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,
    //'finished' => 'hacked_build_report_batch_finished',

    //'file' => drupal_get_path('module', 'hacked') . '/hacked.report.inc',
    'title' => t('Building report'),
  ];
  batch_set($batch);
}

Functions

Namesort descending Description
_hacked_check_project Check core, module or theme.
_hacked_copy_directory Copy directory with files.
_hacked_download_archive Download archive.
_hacked_extract_archive Extract archive.
_hacked_get_projects Get modules and themes.
_hacked_get_temp_dir Create and get temp directory.
_hacked_remove_directory Remove directory with files.
_hacked_report_batch Compute the report data for hacked.
_hacked_restore_project Restore core, module or theme.

Constants

Namesort descending Description
HACKED_OP_CHECK_ALL
HACKED_OP_CHECK_SELECTED @file Contain list of helpers for the module.
HACKED_OP_RESTORE_ALL
HACKED_OP_RESTORE_SELECTED